How to check all checkboxes in table layout in android

I have checked the checkboxes and I want to select all the checkboxes that have been placed in the table layout in android, but it doesn't work for me. Can anyone tell me how to do this, this is my code for selecting all checkboxes in a table:

    public class Manual_AC_Fuse_ckt extends Fragment {

        public int []check_box_count = new int[12];
        CheckBox check_box,
            check_box_count[0] = R.id.cb_1;
                    check_box_count[1] = R.id.cb_2;
                    check_box_count[2] = R.id.cb_3;
                    check_box_count[3] = R.id.cb_4;
                    check_box_count[4] = R.id.cb_5;
                    check_box_count[5] = R.id.cb_6;
                    check_box_count[6] = R.id.cb_7;
                    check_box_count[7] = R.id.cb_8;
                    check_box_count[8] = R.id.cb_9;
                    check_box_count[9] = R.id.cb_10;
                    check_box_count[10] = R.id.cb_11;
                    check_box_count[11] = R.id.cb_12;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
 Root_view = inflater.inflate(R.layout.manualmode_ac_fuse_ckt, null);

        table = (TableLayout)Root_view.findViewById(R.id.table_layout_manual_mode);

        registerForContextMenu(table);
        return Root_view;
                @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("OPTIONS");

        menu.add(0, v.getId(), 0, "SELECT ALL");
        menu.add(0, v.getId(), 0, "DESELECT ALL");
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {

        if(item.getTitle()=="SELECT ALL"){
            for(i=0;i<table.getChildCount();i++){

                check_box_2 = (CheckBox)Root_view.findViewById(check_box_count[i]);
                check_box_2.setChecked(true);


            }

            }

        return super.onContextItemSelected(item);
    }

    }

      

+3


source to share


1 answer


Try it.



@Override
public boolean onContextItemSelected(MenuItem item) {

    if(item.getTitle().equals("SELECT ALL")){
        for(i=0;i<table.getChildCount();i++){

            check_box_2 = (CheckBox)Root_view.findViewById(check_box_count[i]);
            check_box_2.setChecked(true);


        }

        }

    return super.onContextItemSelected(item);
}

      

+1


source







All Articles