android - Getting wrong item image button clicked in listview -


i have listview contains items. , each item has image button. when image button clicked, want change it's image using following code. code change button clicked, changes other items' images not clicked. why happens, , how fix?

 @override     public view getview(int position, view convertview, viewgroup parent) {         view view = convertview;          if (view == null) {             view = linflater.inflate(r.layout.item, parent, false);         }           final imagebutton playbutton = ((imagebutton) view.findviewbyid(r.id.play_pause));          playbutton.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                  // in log correct position of clicked item, image changes wrong.                 log.d(tag, "clicked row-" + view.gettag().tostring());                 ((imagebutton) view.findviewbyid(r.id.play_pause)).setimageresource(r.drawable.button_pause);               }         });          playbutton.settag(position);         return view;     } 

add @ begining of adapter

hashmap<integer,boolean> states = new hashmap<integer, boolean>(); 

and in onclick add this:

if(states.containskey(position)) {    states.put(position, !states.get(position));    ((imagebutton) view.findviewbyid(r.id.play_pause)).setimageresource(states.get(position)?r.drawable.button_pause:r.drawable.button_play); } else {    states.put(position, true);    ((imagebutton) view.findviewbyid(r.id.play_pause)).setimageresource(r.drawable.button_pause); } 

and before return view add this:

if(states.containskey(position) && states.get(position))    ((imagebutton) view.findviewbyid(r.id.play_pause)).setimageresource(r.drawable.button_pause); else    ((imagebutton) view.findviewbyid(r.id.play_pause)).setimageresource(r.drawable.button_play); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -