0

我想创建一个允许多项选择的列表视图。典型的解决方案是获取游标并使用 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 是我的列表视图。

4

2 回答 2

5

我不在 CheckedTextView 上...据我了解,它应该在您单击该行时切换检查。

我想你可以尝试像这样强制它:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {  
    CheckedTextView chkTxt = (CheckedTextView) v.findViewById(R.id.CheckedTextView1); 
    chkTxt.toggle(); 
}

请注意,这将是一个 hack,您应该真正找到根本问题,但同时它可能会让您继续前进。

编辑

经过多次谷歌搜索,我发现了问题......行布局的根需要检查才能使用 CheckedTextView,而 LinearLayout 不是。

更多谷歌搜索找到了解决方案......覆盖 LinearLayout 类。

说明/代码在这里

于 2012-05-20T14:07:22.943 回答
1

“我遇到的问题是复选标记未显示为选中”的问题是因为您的布局错误。它应该没有 LinearLayout:

    <?xml 版本="1.0" 编码="utf-8"?>
    <文本视图
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge" />


    <文本视图
        android:id="@+id/lookup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceSmall" />


    <文本视图
        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" />

然后 SimpleCursorAdapter 可以正确设置您的“checkedTextView1”(具有可见效果)。

就我而言,我使用了 ArrayAdapter 并且它有效。

    list.setAdapter(new ArrayAdapter<String>(this, R.layout.category_layout,
                    R.id.category_checkedText, this.categories));

于 2012-07-08T15:49:47.263 回答