我想创建一个允许多项选择的列表视图。典型的解决方案是获取游标并使用 SimpleCursorAdapter。
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
R.layout.simple_list_item_multiple_choice, cur2, cols2, views2);
使用R.layout.simple_list_item_multiple_choice
. 选择多个项目时,我可以将复选标记工作。
所以我决定尝试使用定制的布局。这是我的布局的 XML 代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="@+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>
</LinearLayout>
所以这是我的问题。使用相同的代码并在我的列表视图上将 ChoiceMode 设置为多个,布局可以很好地膨胀。游标中的数据填充得很好。
但是,我遇到的问题是,当我单击该项目时,复选标记未显示为选中状态(在框中选中)。有什么我遗漏的东西不涉及创建自定义适配器吗?
l2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
l2 是我的列表视图。