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

标签 android listview adapter

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

按照建议here , 我试过了

在我的 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);
        }
    }         
});        

但什么也没发生。

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

<?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>

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

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

谢谢

最佳答案

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

//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;
            }
        }
    }

}

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

关于android - 取消选中自定义 ListView 中的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5312069/

相关文章:

android - 从 Android 应用程序调用 Lua 脚本

android - 突出显示 ListView 项目中的搜索文本

listview - React Native - 当数据未更改时强制 ListView 重新渲染

.net - 如何使用 .Net 应用程序和 SQL 查询访问大型机数据?

notifyDataSetChanged 后未调用 android getChildView

java - 在 Android 上从 mp3 快速转换为 WAV

android - ScrollView 中的相对布局不起作用

android - Android 中的同步适配器

java - 正则表达式匹配器总是返回 false

android - 复选框会在 Android 中自动选中