I am trying to make a contact picker with multiple selection. I am able to load all the contacts with check boxes next to them. However, when I select an item, every 8th item gets selected automatically in my list. Moreover, when I scroll up and down all the selections get changed by itself.
Anybody know what’s wrong with this? Here is my contact picker class
public class HomeActivity extends ListActivity {
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_multiple_choice,
cursor,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
new int[] { android.R.id.text1},0);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView textView = (CheckedTextView)v;
textView.setChecked(!textView.isChecked());
}
}
It happens because of the views recycling mechanism in the
ListView. Try adding the following in youonCreate().