jul jul - 1 year ago 91
Android Question

Uncheck all checkboxes in a custom 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.

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:layout_height="50dp" />

android:layout_height="wrap_content" />
android:layout_height="wrap_content" />


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?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download