Listview button click issue in android -
i'm using list view , there button called "order" in list item. want show "tick" image when user presses button , hide when user presses button again.
my issue when clicked on button on first item, tick image of 4th , 8th item appeared.
this onclicklistener in adapter,
viewholderitem.btnorder.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { boolean isselected = shoe.isselected(); if(isselected){ viewholderitem.ivtick.setimagedrawable(null); isselected = false; }else{ viewholderitem.ivtick.setimagedrawable(mcontext.getresources().getdrawable(r.drawable.tick)); isselected = true; } shoelist.get(position).setselected(isselected); obj.getadapter().notifydatasetchanged(); } });
what reason issue?
update: adapte getview method
public view getview(final int position, view convertview, viewgroup parent) { final viewholderitem viewholderitem; if(convertview == null){ layoutinflater inflater = (layoutinflater)mcontext.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.component_order_shoe_row,null); viewholderitem = new viewholderitem(); viewholderitem.tvname = (textview)convertview.findviewbyid(r.id.tv_component_order_shoe_name); viewholderitem.tvprice = (textview)convertview.findviewbyid(r.id.tv_component_order_shoe_price); viewholderitem.tvdesc = (textview)convertview.findviewbyid(r.id.tv_component_order_shoe_description); viewholderitem.ivpic = (imageview)convertview.findviewbyid(r.id.iv_component_order_shoe_pic); viewholderitem.ivtick = (imageview)convertview.findviewbyid(r.id.iv_component_order_shoe_tick); viewholderitem.spinnersize = (spinner)convertview.findviewbyid(r.id.spinner_component_order_shoe_size); viewholderitem.etqty = (edittext)convertview.findviewbyid(r.id.et_component_order_shoe_qty); viewholderitem.btnorder = (button)convertview.findviewbyid(r.id.btn_component_order_shoe_order); convertview.settag(viewholderitem); }else{ viewholderitem = (viewholderitem)convertview.gettag(); } final shoe shoe = shoelist.get(position); viewholderitem.tvname.settext(shoe.getshoename()); string text = string.valueof(shoe.getprice()); int integerplaces = text.indexof('.'); int decimalplaces = text.length() - integerplaces - 1; if(decimalplaces==1){ viewholderitem.tvprice.settext("rs."+ text + "0"); }else{ viewholderitem.tvprice.settext("rs."+ text); } viewholderitem.tvdesc.settext(shoe.getdesc()); bitmap bmp = appcontrol.convertstringtobitmap(shoe.getimg()); viewholderitem.ivpic.setimagebitmap(bmp); int smallest = shoe.getsmallestsize(); int largest = shoe.getlargestsize(); integer[] arr = getsizearray(smallest,largest); arrayadapter<integer> adapter = new arrayadapter<integer>(mcontext, android.r.layout.simple_spinner_dropdown_item,arr); viewholderitem.spinnersize.setadapter(adapter); viewholderitem.btnorder.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { boolean isselected = shoe.isselected(); if(isselected){ viewholderitem.ivtick.setimagedrawable(null); isselected = false; }else{ viewholderitem.ivtick.setimagedrawable(mcontext.getresources().getdrawable(r.drawable.tick)); isselected = true; } shoelist.get(position).setselected(isselected); obj.getadapter().notifydatasetchanged(); } }); return convertview; }
you have set tag when view created first time so:
if(convertview==null){ layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); //code viewholderitem.btnorder= (imagebutton)rowview.findviewbyid(r.id.imagebutton); viewholderitem.btnorder.settag(viewholder); //set tag button convertview.settag(viewholder); } else { viewholderitem = (viewholder) convertview.gettag(); }
and inside click listener:
viewholderitem.btnorder.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { viewholderitem = (viewholder) v.gettag(); //get tag inside on click boolean isselected = shoe.isselected(); if(isselected){ viewholderitem.ivtick.setimagedrawable(null); isselected = false; }else{ viewholderitem.ivtick.setimagedrawable(mcontext.getresources().getdrawable(r.drawable.tick)); isselected = true; } shoelist.get(position).setselected(isselected); obj.getadapter().notifydatasetchanged(); } });
Comments
Post a Comment