android - ListView with Custom Adapter - IndexOutOfBoundsException when list is empty -
i have list
in store strings server. have custom adapter listview
. each item has textview , switch.
in oncreate method, list empty , after data received server, calling notifydatasetchanged on adapter.
my query specific switch(or checkbox in similar case). please @ code below. best explain using quotes.
public class detaildomactivity extends appcompatactivity { listview listview; dealofthemonthadapter mdealofthemonthadapter; list<string> mtitles = new arraylist<string>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_detail_dom); .... .... // clearing list before getting values server mtitles.clear(); listview = (listview) findviewbyid(r.id.listview); // if remove below 2 lines, // , add them after getting data server, // works fine. // query here is, when list empty, // , when getcount() in adapter returns 0, // why getview() being called? // normal behaviour? mdealofthemonthadapter = new dealofthemonthadapter(this); listview.setadapter(mdealofthemonthadapter); // gets values server , // stores them in mtitles list. // @ end notifydatasetchanged() // called on adapter getdatafromserver(); } private class dealofthemonthadapter extends baseadapter { final context context; list<boolean> mprogress = new arraylist<boolean>(); public dealofthemonthadapter(context context) { this.context = context; mprogress.clear(); } @override public int getcount() { return mtitles.size(); } @override public long getitemid(int position) { return position; } @override public object getitem(int position) { return position; } @override public view getview(final int position, view convertview, viewgroup parent) { final viewholder holder; if (convertview == null) { convertview = layoutinflater.from(context).inflate(r.layout.item_detail_dom, null); holder = new viewholder(); holder.mtextview = (textview) convertview.findviewbyid(r.id.textview); holder.mimageview = (imageview) convertview.findviewbyid(r.id.imageview); holder.mswitch = (switchcompat) convertview.findviewbyid(r.id.switch_dom); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.mtextview.settext(mtitles.get(position)); holder.mswitch.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) mprogress.set(position, true); else mprogress.set(position, false); } }); // indexoutofboundsexception @ below line holder.mswitch.setchecked(mprogress.get(position)); return convertview; } } public static class viewholder { textview mtextview; imageview mimageview; switchcompat mswitch; } }
as far knowledge goes, getview() called depending on count returned getcount(). so, when list empty, should return 0 , hence, getview() should not called.
can please explain me behaviour? let me know if need else.
logcat:
java.lang.indexoutofboundsexception: invalid index 0, size 0 @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255) @ java.util.arraylist.get(arraylist.java:308) @ com.xxx.xx.detaildomactivity$dealofthemonthadapter.getview(detaildomactivity.java:285) @ android.widget.abslistview.obtainview(abslistview.java:2346) @ android.widget.listview.measureheightofchildren(listview.java:1281)
the indexoutofboundsexception
caused mprogress
empty while mtitles
not. dealing multiple collections on listview
can tricky, since have make sure collections have @ least getcount()
's size. that's of course possible, error prone. can wrap information around model class. e.g.
public class mymodel { public string name; public boolean ischecked }
and change list<string> mtitles
list<mymodel> mtitles
. way can deal 1 collection, , retrieve , change property of the item @ position
Comments
Post a Comment