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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With