title: How to remove a selected item from a listview?
body: <p>I would like to know how to remove an item (which a user can select) from a ListView on the User Interface.
  The ListView holds only TextViews which show an IP-Address. Now when I press a button (Remove) I want to remove the selected item from the ListView.</p>
  
  <p>Now I keep track of the selected items through the use of an ArrayList, which hold the indexes of the items. I have set the choiceMode of the ListView to multipleChoice, so these indexes should be accurate.
  I don't know the best way to do it, but my way looks like this:</p>
  
  <pre><code>mEndPointList.setOnItemClickListener(new OnItemClickListener(){
      @Override
      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1,
              int arg2, long arg3){
          boolean found = false;
          int index = 0;
          while(!found &amp;&amp; index &gt;= 0 &amp;&amp; index &lt; mSelectedItems.size()){
              if(mSelectedItems.get(index).intValue() == arg2){
                  found = true;
              }
              index++;
          }
          if(!found){
              mSelectedItems.add(new Integer(arg2));
          }
      }
  });
  </code></pre>
  
  <p>Now when I am finished selecting the Items, I press the Remove button, which should remove the items at the stored Indexes. The code from the button looks like this:</p>
  
  <pre><code>public class RemoveItemButtonHandler implements OnClickListener{
      @Override
      public void onClick(View v){
          for(int index = 0; index &lt; mSelectedItems.size(); index++){
              int selectedItemIndex = mSelectedItems.get(index);
              mEndPointList.removeViews(selectedItemIndex, 1);
          }
  
          mSelectedItems.clear();
          mEndPointList.postInvalidate();
      }
  }
  </code></pre>
  
  <p>This code is added as the onClickListener for the Remove button.
  The code will execute without any problems, but the Item doesn't get removed from the ListView.
  Does anybody have an idea on why it doesn't work?</p>
  
  <hr>
  
  <p>I guess it is only fair to show my solution in case anybody else wonders how I actually did it.
  Selection is done in the <code>OnItemClickListener</code> of the List:</p>
  
  <pre><code>mEndPointList = ((ListView) findViewById(R.id.endpointList));
  mEndPointList.setOnItemClickListener(new OnItemClickListener(){
      @Override
      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1,
              int arg2, long arg3){
          boolean found = false;
          int index = 0;
          /* Loop through all current selected item indexes to see if
           * there is a match. If not, add the index to the list of
           * selected indexes. If the index is already present, remove
           * the index from the list. */
          while(!found &amp;&amp; index &gt;= 0 &amp;&amp; index &lt; mSelectedItems.size()){
              if(mSelectedItems.get(index).intValue() == arg2){
                  found = true;
                  mSelectedItems.remove(index);
              }
              index++;
          }
          if(!found){
              mSelectedItems.add(new Integer(arg2));
          }
          mSelectedItems.trimToSize();
      }
  });
  </code></pre>
  
  <p>Removal of the items is done like this:</p>
  
  <pre><code>public class RemoveItemButtonHandler implements OnClickListener{
      @Override
      public void onClick(View v){
          // Check to see if any items are selected.
          if(mSelectedItems.size() == 0){
              String message = "No items selected.\nTo select, press once on item.\n" +
                               "To unselect, press item again.";
              Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
              return;
          }
          // Sort the selected item indexes from high to low to prevent List corruption.
          Collections.sort(mSelectedItems, new DescendingComparator());
          /* Iterate through the selected items and remove items from the EndPoint List
           * using the selected item index. Corruption of the List is prevented by
           * sorting the selected items list from high to low, thus the item with the
           * highest index is removed first. */
          if(mRawEndPoints.size() &gt; 1){
              for(int index = 0; index &lt; mSelectedItems.size(); index++){
                  int selectedItemIndex = mSelectedItems.get(index);              
                  mRawEndPoints.remove(mListAdapter.getItem(selectedItemIndex));
              }
              // Update the Adapter to notify it's data has changed.
              mListAdapter.notifyDataSetChanged();
          } else {
              String message = "Cannot remove last item from the list.";
              Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
          }
          // Clear the List of selected items for a fresh selection.
          mSelectedItems.clear();
      }
  }
  </code></pre>
  
  <p>Note that the <code>DescendingComparator</code> class is a custom class, which implements the <code>Comparator&lt;Integer&gt;</code> interface.</p>