Wednesday, 9 September 2015

android listview multi-select (multi-choice) clear all checked(selected) items

you can do this to select all items:

    for ( int i=0; i< listView.getCount(); i++ ) {

        listView.setItemChecked(i, true);

    }


You can do this to unselect all selected items:


    for ( int i=0; i< listView.getCount(); i++ ) {

        listView.setItemChecked(i, false);

    }

However, this code will exit listview back to normal state.

One way to unselect all items and still keep in multi-choice mode is:

listView.clearChoices();
((ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();

However, this will refresh the whole list.

Another way to solve it is:

private void batchPickUnselectAll() {

    if (!listView.getAdapter().isEmpty()) {

        listView.setItemChecked(0, true);

        listView.clearChoices();

    }

}









No comments:

Post a comment