How do you activate checkmarks in list activity?

bmalicoat picture bmalicoat · Feb 5, 2010 · Viewed 9.4k times · Source

I have a ListActivity with an array adapter declared like arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_checked); This shows a bunch of rows with checkmarks on the far right. Can you tell me how to get a reference to those checkmarks or how to check/uncheck them?

Answer

Brian Cooley picture Brian Cooley · May 20, 2010

The CheckedTextView itself handles the checkbox. It is passed in as the second argument (View v) in the onListItemClick handler. So, you can simplify your code as follows:

@Override
protected void onListItemClick( ListView l, View v, int position, long id)
{
  CheckedTextView textView = (CheckedTextView)v;
  textView.setChecked(!textView.isChecked());
}