Anton Chertash
Anton Chertash

Reputation: 663

Android ListView::setItemChecked doesn't work

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?

Upvotes: 7

Views: 11310

Answers (1)

jimmithy
jimmithy

Reputation: 6380

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.

Upvotes: 14

Related Questions