7

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

thanks

Yousuf Qureshi
  • 498
  • 2
  • 7
  • 21

4 Answers4

27

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.

bancer
  • 7,475
  • 7
  • 39
  • 58
thom_nic
  • 7,809
  • 6
  • 42
  • 43
21
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.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Abhinav Manchanda
  • 6,546
  • 3
  • 39
  • 46
  • sorted.. thanks so much Abhinav.. much apprecaited – Yousuf Qureshi Dec 30 '10 at 16:14
  • 1
    This code creates some weird behavior when I check all items and then uncheck one item. Solution from Thom Nichols is better. – bancer Mar 30 '12 at 16:07
  • I have buttons in my listview. When click a button outside of listview, I need to change the background of the entire buttons present in the listview. I used your code, and got error. How to achieve this. – Manikandan Jun 20 '12 at 17:52
  • 3
    This checks only the items which are currently in view. So if you have a listView with more items than the view can show, not all items will be checked. The answer from Thom Nichols is indeed the proper solution. – Roy Mar 06 '13 at 11:29
  • working like charm – madhu527 May 10 '16 at 08:44
0

just add one more parameter in adapter as

Boolean Ex.MyAdapter adapter = new Save_Weight_Adapter(this, R.layout.layoutname, array, false);
listview.setAdapter(adapter);

on select all button click listener add this code

MyAdapter adapter = new Save_Weight_Adapter(this, R.layout.layoutname, array, true);
listview.setAdapter(adapter);

and check in adpater

if(chk) {
    blue_check.setVisibility(View.VISIBLE);
} else {
    blue_check.setVisibility(View.GONE);
}
sparticvs
  • 562
  • 5
  • 14
ashish
  • 217
  • 1
  • 11
0

I think you should run this long-running task off the UI thread. When you click button in OnClickListener:

new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < list.getAdapter().getCount(); i++) {
                            final int position = i;
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    list.setItemChecked(pos, true);  
                                }
                            });
                        }
                    }
                }).start();     

and in onCreate() :

this.mHandler = new Handler();

Each item in list view should be Checkable like CheckableRelativeLayout that implements Checkable interface.

Masht Metti
  • 178
  • 1
  • 10