取消选中所有复选框在自定义的ListView [英] Uncheck all checkboxes in a custom ListView

查看:97
本文介绍了取消选中所有复选框在自定义的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个取消全选按钮,在一个ListActivity以选中所有复选框通过自定义SimpleCursorAdapter管理一个ListView。

I'm trying to do an "Unselect all" button in a ListActivity to unchecked all checkboxes in a ListView managed by a custom SimpleCursorAdapter.

作为建议<一href="http://stackoverflow.com/questions/4553186/android-checkbox-listview-select-all-disable-enable">here,我试过

在我ListActivity我有:

In my ListActivity I have:

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

但没有任何反应。

but nothing happens.

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

I'm wondering if this is because of my custom row:

<?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()找不到该复选框。

which makes mListView.setItemChecked() not find the checkbox.

我怎样才能取消所有CB和刷新我的ListActivity从一个按钮,所有的行?

How can I uncheck all cb and refresh all the rows from a button in my ListActivity?

感谢

推荐答案

老实说,我不认为setChecked方法将与自定义布局。该公司预计的观点是与文本1的ID的CheckedTextView。

Honestly I don't think the setChecked Methods will work with a custom layout. It expects the view to be a CheckedTextView with an id of text1.

此外,由于意见被回收,我认为解决的办法是更新任何布尔在列表中,确定是否复选框被选中,然后调用 adapter.notifyDataSetChanged()。您正在改变数据的布尔状态(这是真正重要的东西),并告诉适配器更新的ListView。因此,下一次的意见得出的复选框,将核对无误。这显示目前的意见会重新绘制。

And since the views are recycled I think the solution is to update whatever boolean in your objects in the list that determines if the checkbox is checked and then call adapter.notifyDataSetChanged(). You are changing the boolean state of the data (which is what really matters) and telling the Adapter to update the ListView. So the next time the views are drawn the checkbox will be checked correctly. And the current views that are shown will be redrawn.

这篇关于取消选中所有复选框在自定义的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆