列表视图setItemChecked不起作用视图、不起作用、列表、setItemChecked

2023-09-06 01:40:21 作者:笑看人生

由于某种原因setItemChecked不起作用。可有一个人帮我解决TI?

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"/>

请帮我。

推荐答案

的docs这样说:

设定的选中状态   指定的位置。该才有效   如果选择的模式已被设置为   CHOICE_MODE_SINGLE或   CHOICE_MODE_MULTIPLE。

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.

所以,你可以试试:

list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

之前调用 setItemChecked