Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 881
Anton Chertash Avatar asked Jun 07 '13 17:06

Anton Chertash


1 Answers

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.

like image 66
jimmithy Avatar answered Nov 07 '22 09:11

jimmithy