wrong list items get deleted instead of choosen items with custom list adapter
<p>I've created a custom ListView with checkboxes. I wanted to delete the selected items on "Delete" menu option selection.
   I am handling the onCheckedChanged event on CheckBox. Here I maintain an ArrayList to note down the selected elements position. Now when the user selects the "Delete" menu item I do remove the choosen items from ListAdapter's ArrayList.</p>

<p>Below is my code,</p>

<pre><code>    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    names = new ArrayList&lt;String&gt;();
    names.add("first");
    names.add("second");
    names.add("three");
    names.add("four");
    names.add("five");
    names.add("six");
    names.add("seven");
    names.add("eight");
    names.add("nine");

    listAdapter = new SelfAdapter(this,names);

    this.setListAdapter(listAdapter);                       
}

    @Override
public boolean onOptionsItemSelected(MenuItem menuItem){
    int menuId = menuItem.getItemId();
    ArrayList&lt;Integer&gt; selectedItems;
    switch(menuId){
    case R.id.delitem:
        Log.d(TAG,"del item selected");
        selectedItems = listAdapter.getSelected();
           **//pick the selected position items and delete them from the
           // listadapter arraylist**
        for(Integer element: selectedItems){
          Log.d(TAG,"Selected:"+element.toString());
          names.remove(element.intValue());
          listAdapter.notifyDataSetChanged(); 
        }          
    }
    return true;
}
</code></pre>

<p>Here is the code for custom adapter,</p>

<pre><code>class SelfAdapter extends ArrayAdapter&lt;String&gt;{
ViewHolder holder = new ViewHolder();
private Activity context;
private ArrayList&lt;String&gt; names;
private String TAG = "SelfAdapter"; 
ArrayList&lt;Integer&gt; checkedPos; **//contains the selected items position**
public SelfAdapter(Context context, ArrayList names) {
    super(context,R.layout.checkboxnlist, names);
    this.context = (Activity) context;
    this.names = names;
    checkedPos = new ArrayList&lt;Integer&gt;();
}

@Override
public View getView(int position, View convertView, ViewGroup parent){

    final int listPosition = position;

    if(convertView == null){
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.checkboxnlist,parent,false);            
        holder.chkBox = (CheckBox)convertView.findViewById(R.id.chkvw);
        holder.txtView = (TextView)convertView.findViewById(R.id.txtvw);
        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtView.setText(names.get(position));
    holder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.d(TAG,"listPosition:"+listPosition);
          if(isChecked){
              checkedPos.add(new Integer(listPosition));
          }
          else{
              checkedPos.remove(listPosition);
          }
        }
    });
    return convertView;
}

public ArrayList&lt;Integer&gt; getSelected(){
    return checkedPos;
}

static class ViewHolder{
    TextView txtView;
    CheckBox chkBox;
}
</code></pre>

<p>}</p>

<p>Now the problem is that if select 1st,2nd items (for example) then I see the wrong items get deleted and the listview populate after this is completely wrong. Later if I attempt to delete the items it's throwing "Index out of bounds exception". I guess the code is going wrong in getView method. </p>

<p>Can some one help me in finding out what am doing wrong here.</p>

<p>Thanks</p>
