java - Android baseAdapter stops evaluating if statement on scrolling up -


i have listview contains baseadapter gets data database in json format. of works problem have imagebutton blue background if conditions in if statement evaluate true changes imagebutton background light orange. works when i'm scrolling down listview when scroll listview bunch of other rows don't meet requirement turn image orange. not know how happening code

public class localfeed_customview extends baseadapter {      jsonobject names;     context ctx;     layoutinflater myiflater; activity m;     imagebutton reputation_add;         public localfeed_customview(jsonobject arr,context c) {         ctx = c;         names = arr;     }       @override     public int getcount() {         try {             jsonarray jalocalstreams = names.getjsonarray("localstreams");             return jalocalstreams.length();         } catch (exception e) {             toast.maketext(ctx,"error: please try again",toast.length_long).show();             return names.length();         }       }         @override     public object getitem(int position) {         return position;     }      @override     public long getitemid(int position) {         return position;     }      @override     public view getview(int position,view convertview, viewgroup parent) {         try {             if(convertview==null) {                  layoutinflater li = (layoutinflater) ctx.getsystemservice(context.layout_inflater_service);                 convertview = li.inflate(r.layout.customadapter, null);             }               reputation_add= (imagebutton)convertview.findviewbyid(r.id.reputation_add);               sharedpreferences myaccount= ctx.getsharedpreferences("userinfo", mode_private);             // gets user id , matches data_id come database             int profile_id = myaccount.getint("id",0);                  jsonarray jalocalstreams = names.getjsonarray("localstreams");                 final jsonobject jsonobject = jalocalstreams.getjsonobject(position);                 int data_id=   jsonobject.getint("myid");             // if statement true imagebutton background changes color             if(data_id==profile_id)             {                 reputation_add.setbackgroundresource((r.drawable.borders));             }                return convertview;         } catch (exception e) {             e.printstacktrace();         }         return convertview;       }  } 

as see above when data_id=profile_id evaluate true should imagebutton change background colors , again works when scrolling down listview when going imagebutton turns orange on rows not suppose to. here images clarify more. first & foremost on picture below orange background checkmark is; imagebutton original color blue stated before. to left of image profile_id 32 if by name there number there data_id 32 hence image background turns orange suppose to.

this next image when i'm scrolling , @ numbers on row can see profile_id 32 data_id 0 still turned background orange shouldn't happen because 32==0 false. suggestions appreciated.

i think issue caused because of recycling mechanism of listview. inside getview(), every time have if statement, must set else statement opposite. otherwise, when item recycled (when scroll up, probably), condition happened before , background has changed. you're using same recycled item alternated background , never set original background if condition no longer hold. should be:

... if (condition_holds) {     reputation_add.setbackgroundresource((r.drawable.borders)); } else {     reputation_add.setbackgroundresource((r.drawable.original_background)); } 

in addition, see don't use viewholders , instead call findviewbyid(). il-adviced, if have plenty of views. see previous post me: https://stackoverflow.com/questions/32775508/issue-in-display-phone-contact-list-in-android-application-very-slow/32775803#32775803


Comments