json - Android - displaying wrong data in toast when i click on list view item -
when click on particular id, showing other id data, check below code understand problem
whats wrong code, found not saving position in this, if problem give hints
public feedlistadapter(activity activity, list<feeditem> movieitems) { this.activity = activity; this.feeditems = movieitems; // this.intentcontext = activity; } @override public int getcount() { return feeditems.size(); } @override public object getitem(int location) { return feeditems.get(location); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (inflater == null) inflater = (layoutinflater) activity .getsystemservice(context.layout_inflater_service); if (convertview == null) convertview = inflater.inflate(r.layout.list_news_feed, null); if (imageloader == null) imageloader = volleyapplication.getinstance().getimageloader(); final networkimageview profiepic = (networkimageview) convertview.findviewbyid(r.id.imgprofilepic); final networkimageview postimage = (networkimageview) convertview.findviewbyid(r.id.imgpostimage); personname = (textview) convertview.findviewbyid(r.id.txpersonname); personemail = (textview) convertview.findviewbyid(r.id.txemail); message = (textview) convertview.findviewbyid(r.id.txmessage); createdat = (textview) convertview.findviewbyid(r.id.txcreatedat); final textview weblink = (textview) convertview.findviewbyid(r.id.txweblink); weblink.setpaintflags(weblink.getpaintflags() | paint.underline_text_flag); weblink.settextcolor(color.parsecolor("#0073bd")); postimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string geturl = postimage.getimageurl().tostring(); string name = personname.gettext().tostring(); intent intent = new intent(v.getcontext(), imageviewactivity.class).setflags(intent.flag_activity_new_task); intent.putextra("imageurl", geturl); intent.putextra("name", name); v.getcontext().startactivity(intent); } }); personname.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string name = personname.gettext().tostring(); toast.maketext(personname.getcontext(),name,toast.length_short).show(); } }); postimage.setontouchlistener(new view.ontouchlistener() { private rect rect; @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_down) { postimage.setcolorfilter(color.argb(50, 0, 0, 0)); rect = new rect(v.getleft(), v.gettop(), v.getright(), v.getbottom()); } if (event.getaction() == motionevent.action_up) { postimage.setcolorfilter(color.argb(0, 0, 0, 0)); } if (event.getaction() == motionevent.action_move) { postimage.setcolorfilter(color.argb(0, 0, 0, 0)); } return false; } }); // getting movie data row m = feeditems.get(position); personname.settext(m.getpersonname()); personemail.settext(m.getemail()); message.settext(m.getmessage()); createdat.settext(m.getcreatedat()); imgdelete = (imageview) convertview.findviewbyid(r.id.imgdelete); sharedpreferences preferencesuserdetails = convertview.getcontext().getsharedpreferences("userdetails", convertview.getcontext().mode_private); final string username = preferencesuserdetails.getstring("name", null); final string emailid = preferencesuserdetails.getstring("email", null); string getpersonname = personname.gettext().tostring(); string getpersonemail = personemail.gettext().tostring(); if (getpersonname.equals(username) && getpersonemail.equals(emailid)) { imgdelete.setvisibility(view.visible); } else { imgdelete.setvisibility(view.gone); } txid = (textview) convertview.findviewbyid(r.id.txid); txid.settext(m.getidof()); lilike = (linearlayout) convertview.findviewbyid(r.id.lilike); imglike = (imageview) convertview.findviewbyid(r.id.imglike); txlike = (textview) convertview.findviewbyid(r.id.txlike); txlike.settext(m.getliked()); lilike.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { insertlike(); //toast.maketext(lilike.getcontext(),"dssd",toast.length_short).show(); } }); // thumbnail image profiepic.setimageurl(m.getthumbnailurl(), imageloader); return convertview; } private void insertlike() { final string gettxid = txid.gettext().tostring(); // showprogress(true); final stringrequest postrequest = new stringrequest(request.method.post, url, new response.listener<string>() { @override public void onresponse(string response) { imglike.setimageresource(r.drawable.icn_thumb_up_active); intent = new intent(lilike.getcontext(),mainactivity.class); lilike.getcontext().startactivity(i); toast toast = toast.maketext(lilike.getcontext(), "data inserted successfully"+gettxid, toast.length_short); toast.setgravity(gravity.center, 0, 0); toast.show(); //showprogress(false); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { // showprogress(false); imglike.setimageresource(r.drawable.icn_thumb_up); toast toast = toast.maketext(lilike.getcontext(), "please check internet connection", toast.length_short); toast.setgravity(gravity.center, 0, 0); toast.show(); } }) { @override protected map<string, string> getparams() { sharedpreferences preferencesuserdetails = lilike.getcontext().getsharedpreferences("userdetails", lilike.getcontext().mode_private); final string username = preferencesuserdetails.getstring("name", null); final string emailid = preferencesuserdetails.getstring("email", null); log.w("postid", username); map<string, string> params = new hashmap<string, string>(); params.put("person_name", username); params.put("postid", gettxid); params.put("email_id", emailid); return params; } }; // adding request request queue volleyapplication.getinstance().addtorequestqueue(postrequest); }
i assume problem because you're setting
personname.setonclicklistener
multiple times on same object. when recycle view, using:
if (convertview == null) convertview = inflater.inflate(r.layout.list_news_feed, null);
so lets assume wired view setonclicklistener, after recycling wire again.
a simple solution use index in following method:
personname.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { m = feeditems.get(position); //now need m toast.maketext(personname.getcontext(),m.getpersonname(),toast.length_short).show(); } });
you might want add check know weather object has clicklistener, , not setting multiple times
a possible solution this:
if (convertview == null) { //init views etc... personname.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { } }); } else { //just update data, don't set click listeners }
Comments
Post a Comment