android - When I'm scrolling a list. then click on any view. I'm getting wrong position -
here i'm maintaining list view buttons & edit box. when scroll list , perform onclick operation on button. performed wrongly. when clicked 3rd row selection perform on second row. how can handle
adapter code is
public class itemadapter extends arrayadapter<itemdata> { context context; int layoutresourceid; arraylist<itemdata> data = null; viewholder holder = null; private int selectedpos = -1; // init value not-selected public itemadapter(context context, int layoutresourceid, arraylist<itemdata> data) { super(context, layoutresourceid, data); this.layoutresourceid = layoutresourceid; this.context = context; this.data = data; } public void setselectedposition(int pos) { selectedpos = pos; system.out.println("mainactivity.itemadapter.setselectedposition()-----"+selectedpos); // inform view of change notifydatasetchanged(); } public int getselectedposition() { return selectedpos; } public view getview(int position, final view convertview, final viewgroup parent) { view row = convertview; if (row == null) { layoutinflater inflater = ((activity) context).getlayoutinflater(); row = inflater.inflate(layoutresourceid, parent, false); holder = new viewholder(); holder._itemname = (textview) row.findviewbyid(r.id.item_name); holder._itemprice = (textview) row.findviewbyid(r.id.item_price); holder._list_image = (imageview) row.findviewbyid(r.id.list_image); holder._plusbutton = (button) row.findviewbyid(r.id.plusbutton); holder._minusbutton = (button) row.findviewbyid(r.id.minusbutton); holder._cancelbutton = (button) row.findviewbyid(r.id.cancel); holder._quantityedittext = (textview) row.findviewbyid(r.id.quantityedittext); holder._radio1 = (radiobutton) row.findviewbyid(r.id.firstradio); holder._radio2 = (radiobutton) row.findviewbyid(r.id.secondradio); holder._radio3 = (radiobutton) row.findviewbyid(r.id.thirdradio); system.out.println("mainactivity.itemadapter.getview()-----"+selectedpos); holder._plusbutton.setcontentdescription(position + ""); holder._minusbutton.setcontentdescription(position + ""); } // } else { // holder = (viewholder) row.gettag(); // } final itemdata item_data = data.get(position); system.out.println("mainactivity.itemadapter.getview()--position---"+position); system.out.println("mainactivity.itemadapter.getview()--------name------" + item_data.itemnme); holder._itemname.settext(item_data.itemnme); holder._plusbutton.setonclicklistener(new onclicklistener() { public void onclick(view v) { view singleitemview = mainlistview.getchildat(integer.parseint(v.getcontentdescription().tostring())); textview edittext = (textview) singleitemview.findviewbyid(r.id.quantityedittext); textview pricetaxt = (textview) singleitemview.findviewbyid(r.id.item_price); int itemquantity = (integer.parseint(edittext.gettext().tostring())) + 1; edittext.settext(string.valueof(itemquantity)); edittext.invalidate(); radiobutton button1 = (radiobutton) singleitemview.findviewbyid(r.id.firstradio); radiobutton button2 = (radiobutton) singleitemview.findviewbyid(r.id.secondradio); radiobutton button3 = (radiobutton) singleitemview.findviewbyid(r.id.thirdradio); if (!button1.ischecked() && !button3.ischecked()) button2.setchecked(true); } }); holder._minusbutton.setonclicklistener(new onclicklistener() { public void onclick(view v) { view singleitemview = mainlistview.getchildat(integer.parseint(v.getcontentdescription().tostring())); textview edittext = (textview) singleitemview.findviewbyid(r.id.quantityedittext); textview pricetaxt = (textview) singleitemview.findviewbyid(r.id.item_price); int itemquantity = (integer.parseint(edittext.gettext().tostring())); if (itemquantity != 0) { itemquantity = itemquantity - 1; } edittext.settext(string.valueof(itemquantity)); edittext.invalidate(); radiobutton button1 = (radiobutton) singleitemview.findviewbyid(r.id.firstradio); radiobutton button2 = (radiobutton) singleitemview.findviewbyid(r.id.secondradio); radiobutton button3 = (radiobutton) singleitemview.findviewbyid(r.id.thirdradio); if (itemquantity == 0) { button1.setchecked(false); button2.setchecked(false); button3.setchecked(false); } } }); row.settag(holder); return row; } }
the problem in code in cases when row not null, holder not initialized current element's data , listeners set on holder of previous element or next element. need refactor code fix issue.
Comments
Post a Comment