listview setItemChecked doesn't work

Mark T picture Mark T · Mar 14, 2011 · Viewed 12.6k times · Source

For some reason setItemChecked doesn't work. can some one help me fix ti ?

String[] str = getResources().getStringArray(R.array.brush_type);
sizeArrayAdapter = new ArrayAdapter<String>(this.getContext(), R.layout.drawing_list_item, str);

listType = SIZE_LIST;
listView.setAdapter(sizeArrayAdapter);

// Populate the listView
listView.setItemChecked(4,true);

and this is the list item:

<CheckedTextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawingCheckedTextView"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/listChoiceIndicatorSingle"
        android:background="@drawable/list_panel"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"/>

please help me.

Answer

Matthew Willis picture Matthew Willis · Mar 14, 2011

The docs say this:

Sets the checked state of the specified position. The is only valid if the choice mode has been set to CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE.

So you might try:

list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

before calling setItemChecked.