AddyProg
AddyProg

Reputation: 3050

Selecting All Items in a Listview on checkbox select

I am using simple listView with simple_list_item_multiple_choice I have added a checkbox and on its checked event want all list items to get selected and on unchecked all items to get unselected.. Here is the code..

CheckBox select_all = (CheckBox) dialog.findViewById(R.id.chk_all);
        arrayAdapter = new ArrayAdapter<String>
        (ctx,android.R.layout.simple_list_item_multiple_choice,readyToDownload );
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lv.setAdapter(arrayAdapter);

   select_all.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
            if(select_all.isChecked())
            {
                // check all list items
            }
            if(!select_all.isChecked())
                {
                    //  unselect all list items
                }

            }
                }); 

Upvotes: 0

Views: 24968

Answers (4)

Dusha Kucher
Dusha Kucher

Reputation: 15

Select all / Deselect all / Inverse all

For Activity

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	// Handle action bar item clicks here. The action bar will
	// automatically handle clicks on the Home/Up button, so long
	// as you specify a parent activity in AndroidManifest.xml.
	int id = item.getItemId();

	if (id == R.id.action_select_all) {
		for(int i=0; i < lvDownload.getChildCount(); i++){
			LinearLayout itemLayout = (LinearLayout)lvDownload.getChildAt(i);
			CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.cbFileDownload);
			cb.setChecked(true);
		}
		return true;
	} else if (id == R.id.action_deselect_all) {
		for(int i=0; i < lvDownload.getChildCount(); i++){
			LinearLayout itemLayout = (LinearLayout)lvDownload.getChildAt(i);
			CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.cbFileDownload);
			cb.setChecked(false);
		}
		return true;
	} else if (id == R.id.action_inverse_all) {
		for(int i=0; i < lvDownload.getChildCount(); i++){
			LinearLayout itemLayout = (LinearLayout)lvDownload.getChildAt(i);
			CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.cbFileDownload);
			cb.setChecked(!cb.isChecked());
		}
		return true;
	}

	return super.onOptionsItemSelected(item);
}

lvDownload - ListView ID LinearLayout or RelativeLayout - see root in your item cbFileDownload - CheckBox ID see in your item

And Menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="ua.com.pultok.AboutActivity">
    <item
        android:id="@+id/action_select_all"
        android:orderInCategory="100"
        android:title="@string/action_select_all"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_deselect_all"
        android:orderInCategory="100"
        android:title="@string/action_deselect_all"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_inverse_all"
        android:orderInCategory="100"
        android:title="@string/action_inverse_all"
        app:showAsAction="never" />
</menu>

Upvotes: 0

BAIJU SHARMA
BAIJU SHARMA

Reputation: 296

Call a method from a ListAdapter on a button click or onOptionsItemSelected(MenuItem item).

case  R.id.selectAll:
                listAdapterData.selectAll();
                return true;

case  R.id.unselectAll:
                listAdapterData.unselectAll();
                 return true;

And then,

public class ListAdapterData extends BaseAdapter {
    Context cntxts;
    private LayoutInflater mInflater;
    private ArrayList objects;
    public SparseBooleanArray mSelectedItemsIds;
    boolean[] checkBoxState;
    boolean IsVisibleMain;

   public ListAdapterData(Context context, ArrayList objAll, boolean IsVisible) {
        mInflater = LayoutInflater.from(context);
        this.cntxts = context;
        this.objects = objAll;
        this.mSelectedItemsIds = new SparseBooleanArray();
        checkBoxState = new boolean[objects.size()];
        this.IsVisibleMain = IsVisible;
    }

    public void selectAll() {
        for (int i = 0; i < checkBoxState.length; i++) {
            checkBoxState[i] = true;
        }
        notifyDataSetChanged();
    }

    public void unselectAll() {
        for (int i = 0; i < checkBoxState.length; i++) {
            checkBoxState[i] = false;
        }
        notifyDataSetChanged();
    }
}

Upvotes: 3

Masht Metti
Masht Metti

Reputation: 188

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.

Upvotes: 0

Soumil Deshpande
Soumil Deshpande

Reputation: 1632

for ( int i=0; i < listview.getChildCount(); i++) {
   listview.setItemChecked(i, true);
}

Upvotes: 4

Related Questions