java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 while remove footerview at the end of listview in Android? -
i'm implementing pagination listview. trying add footer view while loading data , remove footer view @ end of listview when reaches on last page on setonscrolllistener @ end of listview.but getting crash reached on last page getting issue of indexoutofboundsexception: invalid index 2, size 2
i'm adding footerview after initialize list
listview listview; int pagecount = 0; view footer;@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); listview = (listview) findviewbyid(r.id.load); // add footer view footer = ((layoutinflater) getsystemservice(context.layout_inflater_service)).inflate(r.layout.prohressbar, null, false); listview.addfooterview(footer); list<all_post> alldesc = dbhelper.getalldescriptions(offset); (all_post all_post : alldesc){ descarray.add(all_post); } if (adapter == null) { adapter = new allpostadapter(this, r.layout.allpostlist, descarray); listview.setadapter(adapter); } if (adapter != null) { adapter.notifydatasetchanged(); adapter = new allpostadapter(this, r.layout.allpostlist, descarray); listview.setadapter(adapter); listview.invalidateviews(); final sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); int item_position = sharedpreferences.getint("item_position", 0); listview.setselection(item_position); } pegination();
and here pagination method
public void pegination() { final int activitycount = sharedpreferences.getint("activitylistcount", 0); log.e("", "activitycount in pegination method =" + activitycount); listview.addfooterview(footer); final button btnloadmore = new button(this); btnloadmore.settext("load more ...."); listview.setonscrolllistener(new abslistview.onscrolllistener() { private int total; @override public void onscrollstatechanged(abslistview abslistview, int scrollstate) { } @override public void onscroll(abslistview abslistview, int firstitem, int visibleitemcount, final int totalitems) { total = firstitem + visibleitemcount; log.e("", "total =" + total); if (totalitems > 0 && total > 0 && total == totalitems) { if (prelast != total) { if (pagecount < localpages) { handler handler = new handler(); handler.postdelayed(new runnable() { public void run() { pagecount++; list<all_post> alldesc = dbhelper.getalldescriptions(offset_scroll); (all_post all_post : alldesc) { descarray.add(all_post); } if (adapter != null) { adapter.notifydatasetchanged(); listview.setadapter(adapter); listview.setselection(totalitems); } } }, 2500); } prelast = total; log.e("", "11111111111 prelast=" + prelast); } prelast = total; log.e("", "222222222222222 prelast=" + prelast); if (pagecount == localpages) { if (prelast == total) { if (activitycount == 200) { log.e(" 11111 in btnloadmore ", " activitylistcount =" + activitycount); log.e("", "go else conditions --- 1 !!!!!! pagecount=" + pagecount + " localpages=" + localpages); pagecount = 0; listview.removefooterview(footer); listview.addfooterview(btnloadmore); } } if (activitycount < 200) { log.e(" 222222 in btnloadmore ", " activitylistcount =" + activitycount); log.e("", "go else conditions --- 2 !!!!!! pagecount=" + pagecount + " localpages=" + localpages); pagecount = 0; listview.removefooterview(btnloadmore); } } } } } }); }
here log cat error information .
04-25 15:48:18.780 28787-28787/com.example.tazeen.classnkk e/﹕ total =137 04-25 15:48:18.780 28787-28787/com.example.tazeen.classnkk e/﹕ 222222222222222 prelast=137 04-25 15:48:18.781 28787-28787/com.example.tazeen.classnkk e/11111 in btnloadmore﹕ activitylistcount =200 04-25 15:48:18.781 28787-28787/com.example.tazeen.classnkk e/﹕ go else conditions --- 1 !!!!!! pagecount=3 localpages=3 04-25 15:48:18.785 28787-28787/com.example.tazeen.classnkk d/androidruntime﹕ shutting down vm 04-25 15:48:18.788 28787-28787/com.example.tazeen.classnkk e/androidruntime﹕ fatal exception: main process: com.example.tazeen.classnkk, pid: 28787 java.lang.indexoutofboundsexception: invalid index 2, size 2 @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255) @ java.util.arraylist.get(arraylist.java:308) @ android.widget.headerviewlistadapter.isenabled(headerviewlistadapter.java:164) @ android.widget.listview.dispatchdraw(listview.java:3334) @ android.view.view.draw(view.java:15364) @ android.widget.abslistview.draw(abslistview.java:4434) @ android.view.view.updatedisplaylistifdirty(view.java:14242) @ android.view.view.getdisplaylist(view.java:14272) @ android.view.view.draw(view.java:15057) @ android.view.viewgroup.drawchild(viewgroup.java:3528) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3322) @ android.view.view.draw(view.java:15364) @ android.view.view.updatedisplaylistifdirty(view.java:14242) @ android.view.view.getdisplaylist(view.java:14272) @ android.view.view.draw(view.java:15057) @ android.view.viewgroup.drawchild(viewgroup.java:3528) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3322) @ android.view.view.updatedisplaylistifdirty(view.java:14234) @ android.view.view.getdisplaylist(view.java:14272) @ android.view.view.draw(view.java:15057) @ android.view.viewgroup.drawchild(viewgroup.java:3528) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3322) @ android.view.view.updatedisplaylistifdirty(view.java:14234) @ android.view.view.getdisplaylist(view.java:14272) @ android.view.view.draw(view.java:15057) @ android.view.viewgroup.drawchild(viewgroup.java:3528) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3322) @ android.view.view.updatedisplaylistifdirty(view.java:14234) @ android.view.view.getdisplaylist(view.java:14272) @ android.view.view.draw(view.java:15057) @ android.view.viewgroup.drawchild(viewgroup.java:3528) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3322) @ android.view.view.updatedisplaylistifdirty(view.java:14234) @ android.view.view.getdisplaylist(view.java:14272) @ android.view.view.draw(view.java:15057) @ android.view.viewgroup.drawchild(viewgroup.java:3528) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3322) @ com.android.internal.policy.impl.phonewindow$decorview.dispatchdraw(phonewindow.java:2845) @ android.view.view.draw(view.java:15364) @ android.widget.framelayout.draw(framelayout.java:647) @ com.android.internal.policy.impl.phonewindow$decorview.draw(phonewindow.java:2824) @ android.view.view.updatedisplaylistifdirty(view.java:14242) @ android.view.view.getdisplaylist(view.java:14272) @ android.view.threadedrenderer.updateviewtreedisplaylist(threadedrenderer.java:267) @ android.view.threadedrenderer.updaterootdisplaylist(threadedrenderer.java:273) @ android.view.threadedrenderer.draw(threadedrenderer.java:312) @ android.view.viewrootimpl.draw(viewrootimpl.java:2852) @ android.view.viewrootimpl.performdraw(viewrootimpl.java:2689) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:2313) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1251) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6438) @ android.view.choreographer$callbackrecord.run(choreographer.java:795) @ android.view.choreographer.docallbacks(choreographer.java:598) @ android.view.choreographer.doframe(choreographer.java:567) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:781) @ android.os.handler.handlecallback(handler.java:810) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:189) @ android.app.activitythread.main(activitythread.java:5529) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:950) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:745)
can please me out issue. why getting invallidindexoutofboundexception .
the index value start zero. first element of array/list 0 e second 1. data has 2 elements, index 2 3 element don't have. code , handle limits correctly.
on line, intead == use <:
if (totalitems > 0 && total > 0 && total < totalitems) {
but validate entire code find possible erros.
Comments
Post a Comment