android - Adapter not getting updated when a value of a view is updated in listview -
i'm using listview show list of elements need count things. list view looks
the value "3" gets updated when click on plus , minus button or reset button. how achieve in customlistviewadapter
holder.plusbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { viewgroup parent = (viewgroup) v.getparent(); textview tv = (textview) parent.findviewbyid(r.id.count); int count = integer.parseint((string) tv.gettext()); count++; tv.settext(string.valueof(count)); int currentval = integer.parseint(totalcount.gettext().tostring()); totalcount.settext(string.valueof(currentval + 1)); } });
where i'm using viewholder pattern.
now problem is, after update value, it's not getting updated in adapter. i.e when try loop on adapter, still value 3 set rather updated value. doing right way or there better way of doing it. how make sure adapter has updated value.
2nd question: adapter not getting updated, when i'm trying retrieve values list send somewhere, nullpointer when
view = listview.getchildat(i).findviewbyid(r.id.count);
for views in list not visible on screen. i.e if have 10 such rows, 5 visible on screen , when try access 6th, null pointer above code block. how should accessing listview values then?
edit: getview method source code is
public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; counter rowitem = getitem(position); layoutinflater minflater = (layoutinflater) context .getsystemservice(activity.layout_inflater_service); if (convertview == null) { convertview = minflater.inflate(r.layout.counter_list, null); holder = new viewholder(); convertview.findviewbyid(r.id.parentlayoutrow).setbackgroundcolor(color.parsecolor(rowitem.getprimarycolor())); holder.resetbutton = (button) convertview.findviewbyid(r.id.buttonreset); holder.countername = (textview) convertview.findviewbyid(r.id.countername); holder.counterdesc = (textview) convertview.findviewbyid(r.id.counterdesc); holder.count = (textview) convertview.findviewbyid(r.id.count); holder.minusbutton = (button) convertview.findviewbyid(r.id.buttonminus); holder.plusbutton = (button) convertview.findviewbyid(r.id.buttonplus); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.resetbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { viewgroup parent = (viewgroup) v.getparent(); textview tv = (textview) parent.findviewbyid(r.id.count); int currentval = integer.parseint(totalcount.gettext().tostring()); totalcount.settext(string.valueof(currentval - integer.parseint(tv.gettext().tostring()))); tv.settext(string.valueof(0)); } }); holder.countername.settext(rowitem.getname()); holder.counterdesc.settext(rowitem.getdesc()); holder.count.settext(string.valueof(rowitem.getcount())); holder.resetbutton.setbackgroundcolor(color.parsecolor(rowitem.getbuttoncolor())); holder.minusbutton.setbackgroundcolor(color.parsecolor(rowitem.getbuttoncolor())); holder.minusbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { viewgroup parent = (viewgroup) v.getparent(); textview tv = (textview) parent.findviewbyid(r.id.count); int count = integer.parseint((string) tv.gettext()); if (count > 0) { count--; tv.settext(string.valueof(count)); int currentval = integer.parseint(totalcount.gettext().tostring()); totalcount.settext(string.valueof(currentval - 1)); } } }); holder.plusbutton.setbackgroundcolor(color.parsecolor(rowitem.getbuttoncolor())); holder.plusbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { viewgroup parent = (viewgroup) v.getparent(); textview tv = (textview) parent.findviewbyid(r.id.count); int count = integer.parseint((string) tv.gettext()); count++; tv.settext(string.valueof(count)); int currentval = integer.parseint(totalcount.gettext().tostring()); totalcount.settext(string.valueof(currentval + 1)); } }); return convertview; }
now how update adapter value in function holder.minusbutton.setonclicklistener(new onclicklistener()
the views in listview don't retain state. viewholder shows thrown away when leave screen , rebuilt later: driven value in adapter.
so want find entry in adapter backing list , update that, have listview redraw itself. have @ listview.getadapter(), adapter.getitem(). you'll note there's no adapter.setitem(), , can't change value of existing string or integer (they're immutable) list item needs object wraps value. or use atomicinteger has increment method. need call listview.notifydatasetchanged() doesn't inspect inside wrapper object.
Comments
Post a Comment