title: How to fix checkboxes auto random selection by scrolling listview, and also shows java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
body: <p>Hi i'm new to android and doing custom listview with checkbox, in that i'm facing random selection of another list item checkbox while scrolling. I have gone through some threads but didn't solve my problem. Pls help me out from this. Here is my custom adapter
              import android.app.Activity;
              import android.app.AlertDialog;
              import android.content.Context;
              import android.content.DialogInterface;
              import android.content.Intent;
              import android.os.Bundle;
              import android.util.SparseBooleanArray;
              import android.view.LayoutInflater;
              import android.view.View;
              import android.view.View.OnClickListener;
              import android.view.ViewGroup;
              import android.widget.BaseAdapter;
              import android.widget.Button;
              import android.widget.CheckBox;
              import android.widget.CompoundButton;
              import android.widget.ListView;
              import android.widget.TextView;
              import android.widget.Toast;</p>
  
  <pre><code>            public class ViewRecord extends Activity {
  
                  DataBaseHelper db;
                  ListView listView;
                  Button delAll, more, setTime;
                  ListAdapter list;
                  CheckBox check;
                  boolean checks = false;
  
                  ArrayList&lt;Contacts&gt; arrayContacts = new ArrayList&lt;Contacts&gt;();
  
                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                      // TODO Auto-generated method stub
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.records);
                      listView = (ListView) findViewById(R.id.listview);
                      delAll = (Button) findViewById(R.id.delete);
                      more = (Button) findViewById(R.id.addMore);
                      setTime = (Button) findViewById(R.id.btnTime);
                      check = (CheckBox) findViewById(R.id.checkBox1);
  
                      loadlist();
  
              //      For adding more contacts to BlockList       
                      more.setOnClickListener(new View.OnClickListener() {
  
                          @Override
                          public void onClick(View v) {
                              // TODO Auto-generated method stub
                              Intent intent = new Intent(ViewRecord.this, ContactList.class);
                              startActivity(intent);
  
                          }
                      });
  
              //      For selecting the whole Records to delete.....
                              check.setOnClickListener(new OnClickListener() {
  
                                  @Override
                                  public void onClick(View v) {
                                      // TODO Auto-generated method stub
                                      list = new ListAdapter(getBaseContext());
                                      checks = !checks;
                                      for (int count =0; count&lt;arrayContacts.size(); count++){
                                      listView.setAdapter(list);
                                      list.mCheckStates.put(count, checks);
                                      list.notifyDataSetChanged();
                                  }
              //                      check.toggle();
                                  }
                              });
                  }
  
                  private void loadlist() {
                      // TODO Auto-generated method stub
                          list = new ListAdapter(this);
                          listView.setAdapter(list);
                          //listView.setOnItemClickListener(this);
                          listView.setItemsCanFocus(false);
                          listView.setTextFilterEnabled(true);
  
  
                  }
  
                  @Override
                  protected void onResume() {
                      // TODO Auto-generated method stub
                      super.onResume();
  
                      arrayContacts.clear();
                      // Reading DataBase Contacts.
                      db = new DataBaseHelper(getApplicationContext());
                      db.getWritableDatabase();
                      ArrayList&lt;Contacts&gt; arrayStor = db.getContacts();
  
                      for (int i = 0; i &lt; arrayStor.size(); i++) {
                          String Id = arrayStor.get(i).getContactId();
                          String Name = arrayStor.get(i).getContactName();
                          String Number = arrayStor.get(i).getContactNumber();
  
                          Contacts contacts = new Contacts();
                          contacts.setContactId(Id);
                          contacts.setContactName(Name);
                          contacts.setContactNumber(Number);
  
                          arrayContacts.add(contacts);
                      }
  
              //      listView.setAdapter(new ListAdapter(this));
                      db.close();
                  }
  
                  private class ListAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {
  
                      private SparseBooleanArray mCheckStates;
                      LayoutInflater inflater;
                      ViewHolder viewHolder;
                      CheckBox cb;
  
                      public ListAdapter(Context context) {
                          mCheckStates = new SparseBooleanArray(arrayContacts.size());
                          inflater = LayoutInflater.from(context);
                      }
  
                      @Override
                      public int getCount() {
                          // TODO Auto-generated method stub
                          return arrayContacts.size();
                      }
  
                      @Override
                      public Object getItem(int position) {
                          // TODO Auto-generated method stub
                          return position;
                      }
  
                      @Override
                      public long getItemId(int position) {
                          // TODO Auto-generated method stub
                          return position;
                      }
  
                      @Override
                      public View getView(int position, View convertView, ViewGroup parent) {
                          // TODO Auto-generated method stub
                          if (convertView == null) {
                              convertView = inflater.inflate(R.layout.listview_row, null);
                              cb = (CheckBox) convertView.findViewById(R.id.checkox);
  
                               cb.setTag(position);
                               cb.setChecked(mCheckStates.get(position, false));
                               cb.setOnCheckedChangeListener(this);
  
                              viewHolder = new ViewHolder();
  
                              viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtdisplaypname);
  
                              convertView.setTag(viewHolder);
                          } else {
                              viewHolder = (ViewHolder) convertView.getTag();
                          }
  
                          viewHolder.txtName.setText(arrayContacts.get(position).getContactName().trim());
  
                          delAll.setOnClickListener(new View.OnClickListener() {
  
                              @Override
                              public void onClick(View v) {
                                  // TODO Auto-generated method stub
                              AlertDialog.Builder alertbox = new AlertDialog.Builder(
                                  ViewRecord.this);
                          alertbox.setCancelable(true);
                          alertbox.setMessage("Are you sure you want to delete ?");
                          alertbox.setPositiveButton("Yes",
                                  new DialogInterface.OnClickListener() {
  
                                      public void onClick(DialogInterface arg0,
                                              int arg1) {
  
                                          if(check.isChecked()==true){
                                              db.emptyRecords();
                                              check.toggle();
                                              ViewRecord.this.onResume();
                                          }else
                                          {
                                          for(int i = 0; i &lt; arrayContacts.size(); i++){
                                                 {
                                                 if(mCheckStates.get(i)==true)
                                                 {         
                                                   db.RemoveRecord(arrayContacts.get(i).getContactId().trim(), "", "");
                                                   System.out.println("The contact name and id is :" + arrayContacts.get(i).getContactId() + arrayContacts.get(i).getContactName());
                                                  ViewRecord.this.onResume();
                                                      }
  
                                                 }
                                                 }
  
                                          Toast.makeText(getApplicationContext()," Records Deleted...",Toast.LENGTH_SHORT).show();
                                          db.close();
                                      }
                                      }
  
                                  });
                          alertbox.setNegativeButton("No",
                                  new DialogInterface.OnClickListener() {
                                      public void onClick(DialogInterface arg0,
                                              int arg1) {
                                      }
                                  });
                          alertbox.show(); 
                          db.close();
                              }
  
  
                          });
  
                          setTime.setOnClickListener(new View.OnClickListener() {
  
                              @Override
                              public void onClick(View v) {
                                  // TODO Auto-generated method stub
  
                                  ArrayList&lt;String&gt; contactName = new ArrayList&lt;String&gt;();
                                  ArrayList&lt;String&gt; contactNumber = new ArrayList&lt;String&gt;();
                                  for(int i = 0; i &lt; arrayContacts.size(); i++)
                                     {
                                     if(mCheckStates.get(i)==true)
                                     {        
  
                                     //   For sending contact names to schedule
                                      contactName.add(arrayContacts.get(i).getContactName());
                                      contactNumber.add(arrayContacts.get(i).getContactNumber()) ;
                                     }         
                                     }                     
                                      Intent sendIntent = new Intent (getApplicationContext(), Time_Settings.class);
                                      sendIntent.putExtra("contactName", contactName);
                                      sendIntent.putExtra("contactNumber", contactNumber);
              //                      Toast.makeText(getApplicationContext(), contactName + contactNumber, 0).show();
                                      startActivity(sendIntent);
                              }
                          });
  
  
  
                          return convertView;
                      }
  
                      public boolean isChecked(int position) {
  
                       return mCheckStates.get(position, checks);
  
                      }
  
                         public void setChecked(int position, boolean isChecked) {
                             mCheckStates.put(position, isChecked);
                             notifyDataSetChanged();
  
                         }
  
                         public void toggle(int position) {
                             setChecked(position, !isChecked(position));
  
                         }
                     @Override
                     public void onCheckedChanged(CompoundButton buttonView,
                             boolean isChecked) {
                       mCheckStates.put((Integer) buttonView.getTag(), isChecked);      
                     }
  
                  }
  
                  private class ViewHolder {
                      TextView txtName;
                  }
              }
                  }
  </code></pre>