android - I have one issue on button click in ListView item -
i have listview
consisting of button "shortlist" , if clicks on button text of button should changed. button shortlist inflation, shortlist[position].tostring() value comes server , if button text shortlisted shown on button , button not pressed. if button text shortlist show on button shortlist , button should pressed.
below code.
public class profile_match_custom_list extends arrayadapter<string> { private static final string url ="http://10.0.2.2/xp/shortlist1.php"; private static final string url1 = "http://10.0.2.2/xp/express_intrest.php"; private static final string key_matri_id_to="matriid_to"; private static final string key_matri_id_by="matriid_by"; sessionmanager session; public string matri_id_to, matri_id_by, str_gender; string str; public button btnsort; private networkimageview imageview; private imageloader imageloader; private final string[] ids; private string[] ages; private string[] heights; public string[] communities; public string[] castes; public string[] educations; public string[] occupations; public string[] incomes; public string[] pics; public string[] locations; public string[] shortlist; public string[] expressinterest; private activity context; public profile_match_custom_list(activity context, string[] ids, string[] ages, string[] heights, string[] communities, string[] castes, string[] educations, string[] occupations, string[]incomes, string[]pics, string[] locations, string[] shortlist, string[] expressinterest) { super(context, r.layout.custom_profile_match,ids); this.ids = ids; this.ages = ages; this.heights = heights; this.communities = communities; this.castes = castes; this.educations = educations; this.occupations = occupations; this.incomes = incomes; this.pics = pics; this.locations = locations; this.shortlist = shortlist; this.expressinterest = expressinterest; this.context = context; } @override public view getview(final int position, view convertview, viewgroup parent) { layoutinflater inflater = context.getlayoutinflater(); final view listviewitem = inflater.inflate(r.layout.custom_profile_match, null, true); // session class instance session = new sessionmanager(getcontext()); session.checklogin(); // user data session hashmap<string, string> user = session.getuserdetails(); matri_id_by = user.get(sessionmanager.key_email); str_gender = user.get(sessionmanager.key_gender); string url1 = "https://www.maangal.com/thumb/thumb_"; string url = url1 + pics[position]; imageview = (networkimageview) listviewitem.findviewbyid(r.id.offer_image); imageloader = customvolleyrequest.getinstance(this.getcontext()).getimageloader(); if(str_gender.equals("male")) { imageloader.get(url, imageloader.getimagelistener(imageview, r.drawable.image, r.drawable.girl)); } else { imageloader.get(url, imageloader.getimagelistener(imageview, r.drawable.image, r.drawable.boy)); } imageview.setimageurl(url,imageloader); textview textviewid = (textview) listviewitem.findviewbyid(r.id.textviewid); textview textviewname = (textview) listviewitem.findviewbyid(r.id.textviewname); textviewid.settext(ids[position]); textviewname.settext( ages[position]+" years"+" , "+heights[position]+" cm"+", "+communities[position]+" : "+castes[position]+" , "+educations[position]+" , "+occupations[position]+" , "+incomes[position]+", "+locations[position]); imageview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(v.getcontext(), blankactivity.class); toast.maketext(getcontext(), ids[position], toast.length_long).show(); i.putextra("id", ids[position]); v.getcontext().startactivity(i); } }); /*button shortlist inflation, shortlist[position].tostring() value comes server , if button text shortlisted shown on button , button not pressed. if button text shortlist show on button shortlist , button should pressed.*/ btnsort =(button) listviewitem.findviewbyid(r.id.btnshort); str = shortlist[position].tostring(); log.e("------shortlisted------",str); if(str.equalsignorecase("shortlisted")) { btnsort.settext(str); btnsort.setbackgroundcolor(color.parsecolor("#ff307668")); btnsort.setenabled(false); } else { btnsort.settext(str); } btnsort.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { matri_id_to=ids[position]; shortlist(); } }); button btnchat =(button) listviewitem.findviewbyid(r.id.btnchat); btnchat.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { matri_id_to=ids[position]; toast.maketext(getcontext(),"sent express intrest",toast.length_long).show(); express_intrest(); } }); button declinebutton = (button)listviewitem.findviewbyid(r.id.declinebutton); declinebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { } }); return listviewitem; } //shortlist method. private void shortlist(){ stringrequest stringrequest = new stringrequest(request.method.post, url, new response.listener<string> () { @override public void onresponse(string response) { log.e("***short response----",response.trim()); if(response.trim().equalsignorecase("success")) { btnsort.setbackgroundcolor(color.parsecolor("#ff307668")); btnsort.settext("shortlisted"); toast.maketext(getcontext(), "shortlisted", toast.length_long).show(); btnsort.setenabled(false); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { toast.maketext(getcontext(), error.tostring(), toast.length_long).show(); } }) { @override protected map<string, string> getparams() { map<string, string> params = new hashmap<string, string>(); params.put(key_matri_id_by,matri_id_by); params.put(key_matri_id_to,matri_id_to); return params; } }; requestqueue requestqueue = volley.newrequestqueue(getcontext()); requestqueue.add(stringrequest); } public void express_intrest(){ stringrequest stringrequest1 = new stringrequest(request.method.post, url1, new response.listener<string>() { @override public void onresponse(string response) { log.e("response",response); toast.maketext(getcontext(), response, toast.length_long).show(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { toast.maketext(getcontext(), error.tostring(), toast.length_long).show(); } }) { @override protected map<string, string> getparams() { map<string, string> params = new hashmap<string, string>(); params.put(key_matri_id_by,matri_id_by); params.put(key_matri_id_to,matri_id_to); return params; } }; requestqueue requestqueue = volley.newrequestqueue(getcontext()); requestqueue.add(stringrequest1); } }
you not refreshing list data. save position click has been done or put data according position.
btnsort.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { matri_id_to=ids[position]; //you can totally discard line can value directly on getparams selectedposition. selectedposition = position; shortlist(); } });
//shortlist method.
private void shortlist(){ stringrequest stringrequest = new stringrequest(request.method.post, url, new response.listener<string> () { @override public void onresponse(string response) { log.e("***short response----",response.trim()); if(response.trim().equalsignorecase("success")) { shortlist[selectedposition] = "shortlisted"; notifydatasetchanged(); //this refresh list , code disabling button work } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { toast.maketext(getcontext(), error.tostring(), toast.length_long).show(); } }) { @override protected map<string, string> getparams() { map<string, string> params = new hashmap<string, string>(); params.put(key_matri_id_by,matri_id_by); params.put(key_matri_id_to,matri_id_to); return params; } }; requestqueue requestqueue = volley.newrequestqueue(getcontext()); requestqueue.add(stringrequest); }
once shortlist[selectedposition] = "shortlisted"
, call notifydatasetchanged()
adapter refresh , getview called. if..else code set btnsort work , find selectedposition in shortlist array "shortlisted" , produce intended output.
Comments
Post a Comment