7

I tried to show simple checked list and I need some items to be checked.

Here is my code

ArrayAdapter<Task> taskAdapter = new ArrayAdapter<Task>(this, android.R.layout.simple_list_item_checked, tasksList); this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); this.getListView().setItemChecked(2, true); setListAdapter(taskAdapter);

<ListView
   android:id="@android:id/list"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

And still it doesn't work. Implementation of Checkable interface didn't help.

What's the trick of this ListView?

Anton Chertash
  • 661
  • 2
  • 7
  • 14

1 Answers1

14

You need to set the adapter before setting the item as checkable.

ArrayAdapter<Task> taskAdapter = new ArrayAdapter<Task>(this, android.R.layout.simple_list_item_checked, tasksList);
setListAdapter(taskAdapter);        
this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
this.getListView().setItemChecked(2, true); 

The adapter contains the data stored in the listview so Item 2 does not exist in the listview until the adapter is set.

jimmithy
  • 6,360
  • 2
  • 31
  • 29