Listview自定义布局多项选择

时间:2012-05-20 13:31:25

标签: android xml listview simplecursoradapter

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

所以这是我的问题。使用相同的代码并在我的listview上将ChoiceMode设置为多个,可以很好地扩展布局。光标中的数据填充正确。

但是,我遇到的问题是,当我点击该项目时,复选标记不会显示为选中(框中的复选框)。是否有一些我不想要的东西不涉及创建自定义适配器?


l2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

l2是我的列表视图。

2 个答案:

答案 0 :(得分:5)

我没有使用CheckedTextView ...根据我的理解,它应该在您点击该行时切换检查。

我想你可以尝试强制它:

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

请注意,这将是一个黑客,你应该真的找到根本问题,但它可能会让你在此期间。

修改

经过大量的谷歌搜索后,我发现了问题......行布局的根目录需要检查才能使用CheckedTextView,而LinearLayout则不行。

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

说明/代码here

答案 1 :(得分:1)

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


    <?xml version="1.0" encoding="utf-8"?>
    <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" />

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

在我的情况下,我使用了ArrayAdapter并且它有效。


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