I have a Checked ListView with a few items in it. Here’s just a quick example of how I’m creating it:
public class SettingsActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> settingsitems = new ArrayList<String>();
settingsitems.add("Item A");
settingsitems.add("Item B");
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, settingsitems));
...
How do I set a default item to be checked when the list is loaded? For example, when the list is shown I’d like Item B to be checked only.
I’ve tried adding the following before and after setListAdapter and it doesn’t work:
this.getListView().setItemChecked(1, true);
the docs for the setItemChecked() method state:
the default choice mode for the ListView is CHOICE_MODE_NONE which means that if you do not explicitly set it to either SINGLE or MULTIPLE your call to setItemChecked() will not do anything.
so try adding this line before you call setItemChecked():