fetching checkbox state in a gridview item for all checkbox in gridview on button click

前端 未结 1 1979
别跟我提以往
别跟我提以往 2021-01-28 13:27

I have the code below, now I need to keep track of the checkbox state in each gridview item, and fetch that info on a button click to update the information. My button event in

相关标签:
1条回答
  • 2021-01-28 13:35

    Deal with integer array to store the state of your checkboxes when it checked/unchecked, Initially fill the array with 0 values which indicate unchecked of your checkboxes like this.

    int[] checkStates;
     checkStates = new int[datalist.length()];
       for (int i = 0; i < datalist.length(); i++) {
            checkStates[i] = 0;
       }
    

    Now handle checkboxes click event to get perfect position. use settag & get gettag and inside click event when box get selected change the value of particular position to 1 from 0.

    Like this#

      checkbox.setTag(position);
    
    
    
        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                pos = (Integer) buttonView.getTag();
                // checkStates[pos] = 1;
                // pos = (Integer) v.getTag();
    
                if (!buttonView.isChecked()) {
                    boxState[pos] = 0;
                } else {
                    boxState[pos] = 1;
                }
                notifyDataSetChanged();
            }
        });
    

    and inside getview method handle your check/uncheck state this way..

    if (checkStates[position] == 0) {
                checkbox.setChecked(false); // set unchecked"
            } else {
                checkbox.setChecked(true); // set checked"
            }
    

    This way you will get the info of checkboxes which are selected, further handle your button click event, and get the int array which filled while check and uncheck.

    0 讨论(0)
提交回复
热议问题