Listview自定义布局多项选择

Arn*_* C. 0 xml android listview simplecursoradapter

我想创建一个允许多项选择的列表视图.典型的解决方案是获取游标并使用SimpleCursorAdapter.

        SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
                    R.layout.simple_list_item_multiple_choice, cur2, cols2, views2);
Run Code Online (Sandbox Code Playgroud)

使用时,我能够正常工作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>
Run Code Online (Sandbox Code Playgroud)

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

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


l2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Run Code Online (Sandbox Code Playgroud)

l2是我的列表视图.

Bar*_*rak 5

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

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

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

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

编辑

经过大量的谷歌搜索,我发现问题...行布局的根需要检查使用CheckedTextView,而LinearLayout不是.

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

说明/代码在这里