取消选中自定义ListView中的所有复选框

jul*_*jul 6 android listview adapter

我正在尝试在ListActivity中执行"取消全选"按钮,以取消选中由自定义SimpleCursorAdapter管理的ListView中的所有复选框.

正如这里所建议的,我试过了

在我的ListActivity中,我有:

Button bt_f_unsel = (Button) findViewById(R.id.btn_f_unsel);
bt_f_unsel.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {           
        for ( int i=0; i< getListAdapter().getCount(); i++ ) {
            mListView.setItemChecked(i, false);
        }
    }         
});        
Run Code Online (Sandbox Code Playgroud)

但没有任何反应.

我想知道这是不是因为我的自定义行:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/contact_pic"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/contact_name"        
        android:textSize="10sp"
        android:singleLine="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckBox
        android:id="@+id/checkbox"
        android:button="@drawable/whipem_cb"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这使得mListView.setItemChecked()找不到复选框.

如何取消选中所有cb并刷新ListActivity中按钮的所有行?

谢谢

hal*_*ate 6

我正在使用一个肮脏但简单的技巧:

//recursive blind checks removal for everything inside a View
private void removeAllChecks(ViewGroup vg) {
    View v = null;
    for(int i = 0; i < vg.getChildCount(); i++){
        try {
            v = vg.getChildAt(i);
            ((CheckBox)v).setChecked(false);
        }
        catch(Exception e1){ //if not checkBox, null View, etc
            try {
                removeAllChecks((ViewGroup)v);
            }
            catch(Exception e2){ //v is not a view group
                continue;
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

将您的列表对象传递给它。只是避免非常长和复杂的列表。


Rob*_*ond 4

老实说,我认为 setChecked 方法不适用于自定义布局。它期望视图是 ID 为 text1 的 CheckedTextView。

由于视图被回收,我认为解决方案是更新列表中对象中的任何布尔值,以确定是否选中复选框,然后调用adapter.notifyDataSetChanged(). 您正在更改数据的布尔状态(这才是真正重要的)并告诉适配器更新 ListView。因此,下次绘制视图时,将正确选中该复选框。当前显示的视图将被重新绘制。