title: Delete Multiple Images in GridView android
body: <p>I am building an app for practice purpose , so it goes like this that Images will be shown on screen in a gridView and :-</p>
  
  <p>Below is the Screenshot of the app (done till now):-</p>
  
  <p><a href="http://i.stack.imgur.com/8re7R.jpg" rel="nofollow">ScreenShot</a></p>
  
  <p>-if user clicks on an image another activity opens which shows the image that was clicked in fullscreen. (<strong>done</strong> this part)</p>
  
  <p>-if user long presses an image , the Image is selected and user can select multiple Images.  (<strong>done</strong> this part)</p>
  
  <p>-There is a menu on the Screen containing Delete icon/item and Set as Wallpaper icon/item. (<strong>done</strong> this part)</p>
  
  <p>If User clicks on the delete icon of menu ,all the selected images should be deleted (<strong>NOT DONE  , I am stuck in this part</strong> ).</p>
  
  <p>^ As you can see in this Image , i have selected two Images now if i click on the RecycleBin icon on menu , it only deletes ONE Image the starting one (pink color) i.e I am trying to delete the Images in Main Activity (In this case it's name is ImagesShow) and then calling notifyDataSetChanged within the loop or outside the loop but it only runs one time and deletes one image altough in for loop it detects that at the moment Number of Images Selected = 2. Below is a snippet of code for this part specifically , if someone need more detailed code in order to answer then i can also provide it.</p>
  
  <pre><code>//In MainActivity 
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId())
  
      {
  
          case R.id.deleteImages:
  
          //Inside RecycleBin Click
          //CountSelectedImages simply counts the Number of Images currently Selected
              if (CountSelectedImages() &gt; 0)
              {
                //Images_List is an ArrayList of type Images where Images is an class containing ImagePath as well as boolean condition of IsSelected
                  for (int x = 0,i=0; x &lt; Images_list.size(); x++) 
                  {
  
                              if (Images_list.get(x).IsSelected())
                              {
                                //In our above example , we should arive in this location 2 times.
  
                                  Images_list.remove(x);
  
                                //I even tried to write NotifyDataSetChanged here in this if condition just after Removing the Images_List. 
                                //But it still runs one time only , only deleting the first Image that is Selected and never coming back inside this if condition.
                                  ab.setSubtitle(CountSelectedImages() + "/" + Images_list.size() + " items Selected");
  
                              }
  
                    }
  
                      //So i read somewhere that we need to call notifyDataSetChanged once we have done all the changes in the array, so here is my final attempt but still it only runs for one time deleting only single image.
                              ((CustomImageAdapter) gridView.getAdapter()).notifyDataSetChanged(); //Can also write BaseAdapter instead of CustomImageAdapter
                               ClearWholeSelection(); //This functions clears the Selection of all Images , just as it happens in normal gallery
              }//end of if
      }//end of switch
  }
  </code></pre>
  
  <p>However There is one glitch in this , i think if i try to catch it i might solve the problem.</p>
  
  <p>Take this example:- 
  GridView Currently contains following Views:- </p>
  
  <h2>0 1</h2>
  
  <h2>2 3</h2>
  
  <h2>4 5</h2>
  
  <p><strong>Now if i select ImageView 0 and 1 and press delete , it deletes only 0.
  BUT if i press 0 , 2 and 4 , IT DELETES 0 , 2 and 4  ( Magic ? ) i.e IT WORKS FINE IN THIS CASE and also if i try to delete 1 , 3 , 5 
  It 's somehow able to multiple delete images in a column but when it comes to row wise deletion it only deletes one image.</strong> </p>
  
  <p>Other Method i tried was to make a function of Remove(Image) in CustomImageAdapter, and in the above for loop just call , Remove (Images_List.get(x)). 
  and in Remove(Image) i try to remove the Image in CustomImageAdapter List (same as Images_List) and then call NotifyDataSetChanged from the CustomImageAdapter.</p>
  
  <p>Annnnnnd it also didn't worked.</p>
  
  <p>If someone can solve this problem or explain the working of NotifyDataSetChanged in baseAdapter then please write your answer , need to clear my concept.</p>
  
  <p>And there is also one interesting thing , i read in stackoverflow that getview() is called in a loop depending upon the getCount () function that you override , But then it totally blows my mind when i write in my Main Activity </p>
  
  <pre><code>Images_List.get(x).ToggleSelection();
  
  ((BaseAdapter)gridView.getAdapter()).notifyDataSetChanged();
  </code></pre>
  
  <p>Annd it somehow runs the getView successfully and shows the "Selected" text on an image as you can see in the Screenshot Above , but the thing is that the count i.e Images_list.size() remains the same so why did the notifyDataSetChanged runs Successfully here??</p>
  
  <p>P.S Sorry for Long Post :)</p>
  
  <p>EDIT-------------SOLVED</p>
  
  <p>Problem in this case is a Logical Error as i am iterating through the Images_List Array while also removing from it. 
  Thanks to Ben's Answer (using Iterator) , my code works fine now. </p>