title: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 while remove footerview at the end of listview in Android?
body: <p>I'm implementing pagination with listview. I trying to add footer view while loading my some data and remove footer view at the end of listview when reaches on the last page on setOnScrollListener at the end of listview.But getting crash reached on the last page getting issue of IndexOutOfBoundsException: Invalid index 2, size is 2</p>
  
  <p>I'm adding footerview after initialize the list</p>
  
  <pre><code>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&lt;All_Post&gt; allDesc = dbhelper.getAllDescriptions(OFFSET);
      for (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();    
  </code></pre>
  
  <p>And here is my pagination method </p>
  
  <pre><code>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 &gt; 0 &amp;&amp; total &gt; 0 &amp;&amp; total == totalItems) {
                      if (preLast != total) {
                          if (pageCount &lt; LocalPages) {
                              Handler handler = new Handler();
                              handler.postDelayed(new Runnable() {
                                  public void run() {
                                      pageCount++;
  
                                      List&lt;All_Post&gt; allDesc = dbhelper.getAllDescriptions(OFFSET_SCROLL);
                                  for (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 to else conditions --- 1 !!!!!!  pageCount=" + pageCount + " LocalPages=" + LocalPages);
                                  pageCount = 0;
  
                                  listView.removeFooterView(footer);
                                  listView.addFooterView(btnLoadMore);
                              }
                          }
                              if (activityCount &lt; 200) {
                                  Log.e(" 222222 In btnLoadMore ", " ActivityListCount =" + activityCount);
                                  Log.e("", "Go to else conditions --- 2 !!!!!!  pageCount=" + pageCount + " LocalPages=" + LocalPages);
                                  pageCount = 0;
                                  listView.removeFooterView(btnLoadMore);
  
  
                              }
                          }
  
  
                      }
                  }
              }
  
          });
  
      }
  </code></pre>
  
  <p>Here is my Log Cat error information .</p>
  
  <pre><code>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 to 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 is 2
              at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
              at java.util.ArrayList.get(ArrayList.java:308)
              at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
              at android.widget.ListView.dispatchDraw(ListView.java:3334)
              at android.view.View.draw(View.java:15364)
              at android.widget.AbsListView.draw(AbsListView.java:4434)
              at android.view.View.updateDisplayListIfDirty(View.java:14242)
              at android.view.View.getDisplayList(View.java:14272)
              at android.view.View.draw(View.java:15057)
              at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
              at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
              at android.view.View.draw(View.java:15364)
              at android.view.View.updateDisplayListIfDirty(View.java:14242)
              at android.view.View.getDisplayList(View.java:14272)
              at android.view.View.draw(View.java:15057)
              at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
              at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
              at android.view.View.updateDisplayListIfDirty(View.java:14234)
              at android.view.View.getDisplayList(View.java:14272)
              at android.view.View.draw(View.java:15057)
              at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
              at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
              at android.view.View.updateDisplayListIfDirty(View.java:14234)
              at android.view.View.getDisplayList(View.java:14272)
              at android.view.View.draw(View.java:15057)
              at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
              at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
              at android.view.View.updateDisplayListIfDirty(View.java:14234)
              at android.view.View.getDisplayList(View.java:14272)
              at android.view.View.draw(View.java:15057)
              at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
              at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
              at android.view.View.updateDisplayListIfDirty(View.java:14234)
              at android.view.View.getDisplayList(View.java:14272)
              at android.view.View.draw(View.java:15057)
              at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
              at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
              at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchDraw(PhoneWindow.java:2845)
              at android.view.View.draw(View.java:15364)
              at android.widget.FrameLayout.draw(FrameLayout.java:647)
              at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2824)
              at android.view.View.updateDisplayListIfDirty(View.java:14242)
              at android.view.View.getDisplayList(View.java:14272)
              at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:267)
              at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:273)
              at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:312)
              at android.view.ViewRootImpl.draw(ViewRootImpl.java:2852)
              at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2689)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2313)
              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6438)
              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:795)
              at android.view.Choreographer.doCallbacks(Choreographer.java:598)
              at android.view.Choreographer.doFrame(Choreographer.java:567)
              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:781)
              at android.os.Handler.handleCallback(Handler.java:810)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:189)
              at android.app.ActivityThread.main(ActivityThread.java:5529)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
  </code></pre>
  
  <p>Can someone please help me out this issue. Why getting InvallidIndexOutOfBoundException .</p>