title: Android : java.lang.IndexOutOfBoundsException: Invalid index 5, size is 4
body: <p>I'm implementing to deletion the multiple images from Grid-view on delete button click . I have put check box and image-view in Grid-View.I select multiple check box on images those images are deleted but sometime my application was crashed and getting exception of <strong>java.lang.IndexOutOfBoundsException: Invalid index 5, size is 4</strong> at this line = <strong>data.remove(pos);</strong> in check box click event.How to solve this .</p>
  
  <p>Here is my code in Adapter class </p>
  
  <pre><code>final BitmapItems item = data.get(position);
          //holder.imageTitle.setText(item.getTitle());
          holder.image.setImageBitmap(item.getBitmap());
              final int pos = position;
              Log.e("pos ", " = " + pos);
  
              holder.checkbox.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v)
                  {
                      CheckBox cb = (CheckBox) v;
                      Toast.makeText(getContext(), "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
                      if(cb.isChecked())
                      {
                          int checked = 1;
                          Log.e("", "" + checked);
                          String path = ((String) f.get(pos));
                          selectedFile.add(path);
                          Log.e("path ", " = " + path);
                          data.remove(pos);
                      }
                      else
                      {
                          int checked = 0;
                          Log.e("",""+ checked);
                      }
                  }
              });
  </code></pre>
  
  <p>Here is my delete button code in Activity</p>
  
  <pre><code>imgDeleteImagesFromGallery = (ImageView)findViewById(R.id.imgDeleteImage);
          imgDeleteImagesFromGallery.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v)
              {
                  gridAdapter.notifyDataSetChanged();
  
                  Iterator iterator = selectedFile.iterator();
                  while (iterator.hasNext())
                  {
                      gridFilePath = new File(iterator.next().toString());
                      if(gridFilePath.exists())
                      {
                          gridFilePath.delete();
                          Log.e("File deleted","Succesfully !!");
                      }
                  }
              }
          });
  </code></pre>
  
  <p>Here Log Cat error</p>
  
  <pre><code>10-14 04:16:02.635  15018-15018/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.example.tazeen.classnkk, PID: 15018
      java.lang.IndexOutOfBoundsException: Invalid index 5, size is 4
              at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
              at java.util.ArrayList.remove(ArrayList.java:403)
              at com.example.tazeen.classnkk.AddPost$GridView_Adapter$1.onClick(AddPost.java:931)
              at android.view.View.performClick(View.java:4756)
              at android.widget.CompoundButton.performClick(CompoundButton.java:125)
              at android.view.View$PerformClick.run(View.java:19748)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5254)
              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:898)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
  </code></pre>