Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Checkbox listview select all (disable/enable) [duplicate]

I want to disable/enable all checkboxes in listview. infact want to get select all behaviour by clicking on top checkbox.

thanks

like image 938
Yousuf Qureshi Avatar asked Dec 29 '10 10:12

Yousuf Qureshi


2 Answers

This is what finally worked for me, where I'm using a cursor adapter, not just an ArrayListAdapter for my list items:

final ListView list = getListView();
for ( int i=0; i< getListAdapter().getCount(); i++ ) {
        list.setItemChecked(i, true);
}

list.getChildCount doesn't work because it only seems to count what's been drawn immediately (not everything that's off the screen) so childCount might only be 6 or 8 items when the entire list is 100 or more items. Also as I had to use list.setItemChecked to get the items to 'stay checked' -- at least in my case where my list items were instances of CheckedTextView.

like image 75
thom_nic Avatar answered Nov 08 '22 21:11

thom_nic


for(int i=0; i < listView.getChildCount(); i++){
    RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i);
    CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox);
    cb.setChecked(true);
}

You need to edit this code to handle both enabling and disabling, but I hope you get the idea !

Also, this only checks each checkbox, make sure you are returning an id or object from your list in order to send/save the data elsewhere.

like image 27
Abhinav Manchanda Avatar answered Nov 08 '22 21:11

Abhinav Manchanda