6

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

As suggested here, I tried

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>

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

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

Thanks

Community
  • 1
  • 1
jul
  • 36,404
  • 64
  • 191
  • 318

4 Answers4

6

I'm using a dirty but easy trick:

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

}

Pass your list object to it. Just avoid really long and complicated lists.

halxinate
  • 1,509
  • 15
  • 22
4

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.

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.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
3

This worked for me:

    MenuViewAdapter adapter = new MenuViewAdapter(this, menuViews,this);
ListView lv = (ListView)this.findViewById(R.id.menu_list);


CheckBox cb;

for(int i=0; i<lv.getChildCount();i++)
{
    cb = (CheckBox)lv.getChildAt(i).findViewById(R.id.checkBox);
    cb.setChecked(false);
}
adapter.notifyDataSetChanged();
hrushi
  • 294
  • 1
  • 10
  • Using this code, i am able to uncheck only visible Checkbox if i have more than 100 item. Have you tried out to uncheck first item and last item of the list? – Google Mar 08 '17 at 11:06
  • This unchecks all checkboxes in the list – hrushi Mar 28 '17 at 22:55
  • i did the same trick but unable to unchecked all checkbox. i unchecked only visible item. but when i scroll down the list some checkbox are not unchecked – Google Mar 30 '17 at 12:27
-1

I use

checkbox.setChecked(false);
checkbox.refreshDrawableState();
Ben
  • 51,770
  • 36
  • 127
  • 149
widomin
  • 19
  • 3
  • 2
    Hi, and welcome to Stack Overflow. If you highlight code and click the `{}` button it will be highlighted automatically for you. – Ben Aug 21 '13 at 21:28