Selecting All Items in a Listview on checkbox select

后端 未结 4 1503
北恋
北恋 2020-12-06 15:46

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

4条回答
  •  不知归路
    2020-12-06 16:18

    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();
        }
    }
    

提交回复
热议问题