Clear all checkboxes in custom ListView

I am trying to make the "Unselect all" button in a ListActivity to uncheck the boxes in a ListView controlled 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 am wondering if this is due to my user line:

 <?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 msistView.setItemChecked () not set.

How can I strip all cb and update all rows using a button in ListActivity?

thanks

+6
android listview adapter
source share
4 answers

Honestly, I don't think setChecked Methods will work with custom layouts. He expects the view to be a CheckedTextView with id text1.

And since the views are redesigned, I think the solution is to update any boolean in your objects in the list, which determines if the checkbox is checked, and then calls adapter.notifyDataSetChanged() . You change the logical state of the data (which is really important) and tell the adapter to update the ListView. Therefore, at the next viewing of views, the checkbox will be correctly checked. And the current viewed images will be redrawn.

+4
source share

I use a dirty but simple 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 it your list object. Just avoid very long and complicated lists.

+3
source share

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(); 
+2
source share

I use

 checkbox.setChecked(false); checkbox.refreshDrawableState(); 
-one
source share

All Articles