title: Removing elements on ArrayList throw an IndexOutOfBoundsException on ArrayAdapter
body: <p>I'm developing an Android 3.1 application.</p>
  
  <p>I have my custom ArrayAdapter. I want to show a list of names in a ListView.</p>
  
  <p>These names are forms that could be download and save it locally. When user download and save one or more, I call <code>updateFormsNotDownloaded()</code>. But when I do that I get an IndexOutOfBoundsException. And I think this problem is because I call <code>notifyDataSetChanged()</code>.</p>
  
  <p>Look at my code:</p>
  
  <pre><code>public class FormAdapter extends ArrayAdapter&lt;Form&gt;
  {
      private Context context;
      private int layoutResourceId;
      private List&lt;Form&gt; forms;
      private ArrayList&lt;Integer&gt; checkedItemsPosition;
      private Button downloadButton;
  
      public ArrayList&lt;Integer&gt; getCheckedItemsPosition()
      {
          return checkedItemsPosition;
      }
  
      public String[] getSelectedFormsId()
      {
          String[] ids = new String[checkedItemsPosition.size()];
          int i = 0;
          for(Integer pos : checkedItemsPosition)
          {
              Form f = forms.get(pos.intValue());
              ids[i] = f.FormId;
              i++;
          }
          return ids;
      }
  
      /**
       * Called when selected forms has been downloaded and save it locally correctly.
       */
      public void updateFormsNotDownloaded()
      {
          ArrayList&lt;Form&gt; copyForms = new ArrayList&lt;Form&gt;();
          for (int i = 0; i &lt; forms.size(); i++)
          {
              if (!checkedItemsPosition.contains(new Integer(i)))
                  copyForms.add(forms.get(i));
          }
          forms = copyForms;
          checkedItemsPosition.clear();
          notifyDataSetChanged();
      }
  
      public FormAdapter(Context context, int textViewResourceId,
              List&lt;Form&gt; objects, Button downloadButton)
      {
          super(context, textViewResourceId, objects);
  
          this.context = context;
          this.layoutResourceId = textViewResourceId;
          this.forms = objects;
          this.checkedItemsPosition = new ArrayList&lt;Integer&gt;();
          this.downloadButton = downloadButton;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent)
      {
          View row = convertView;
          if (row == null)
          {
              LayoutInflater inflater = ((Activity)context).getLayoutInflater();
              row = inflater.inflate(layoutResourceId, parent, false);
          }
  
          Form f = forms.get(position);
          if (f != null)
          {
              CheckBox checkBox = (CheckBox)row.findViewById(R.id.itemCheckBox);
              if (checkBox != null)
              {
                  checkBox.setText(f.Name);
                  checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
                  {
                      public void onCheckedChanged(CompoundButton buttonView,
                              boolean isChecked)
                      {
                          //Form f = forms.get(position);
                          if (isChecked)
                          {
                              //checkedItems.add(f.FormId);
                              checkedItemsPosition.add(new Integer(position));
                          }
                          else
                          {
                              //checkedItems.remove(checkedItems.indexOf(f.FormId));
                              checkedItemsPosition.remove(checkedItemsPosition.indexOf(new Integer(position)));
                          }
                          downloadButton.setEnabled(checkedItemsPosition.size() &gt; 0);
                      }
                  });
              }
          }
  
          return row;
      }
  }
  </code></pre>
  
  <p>I had three items on forms, but I remove one of them.</p>
  
  <p>Why am I getting that exception?</p>
  
  <p>This is exception log:</p>
  
  <pre><code>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 es.viacognita.adapters.FormAdapter.getView(FormAdapter.java:89)
  at android.widget.AbsListView.obtainView(AbsListView.java:1949)
  at android.widget.ListView.makeAndAddView(ListView.java:1756)
  at android.widget.ListView.fillDown(ListView.java:656)
  at android.widget.ListView.fillSpecific(ListView.java:1314)
  at android.widget.ListView.layoutChildren(ListView.java:1587)
  at android.widget.AbsListView.onLayout(AbsListView.java:1800)
  at android.view.View.layout(View.java:9581)
  at android.view.ViewGroup.layout(ViewGroup.java:3877)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1314)
  at android.view.View.layout(View.java:9581)
  at android.view.ViewGroup.layout(ViewGroup.java:3877)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
  at android.view.View.layout(View.java:9581)
  at android.view.ViewGroup.layout(ViewGroup.java:3877)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1314)
  at android.view.View.layout(View.java:9581)
  at android.view.ViewGroup.layout(ViewGroup.java:3877)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
  at android.view.View.layout(View.java:9581)
  at android.view.ViewGroup.layout(ViewGroup.java:3877)
  at android.view.ViewRoot.performTraversals(ViewRoot.java:1253)
  at android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:132)
  at android.app.ActivityThread.main(ActivityThread.java:4025)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:491)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>