Android ListView :: setItemChecked not working

I tried to show a simple checklist, and I need to check some items.

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 yet it will not work. Implementing the Checkable interface did not help.

What is the trick of this ListView?

+7
source share
1 answer

You need to install the adapter before installing the item as verifiable.

 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 list, so item 2 does not exist in the list until the adapter is installed.

+14
source

All Articles