How to get all checkboxes value from dynamically view in android

Viewed 25

The following are my code, I have checkboxes in the layout binding (filterManagerBinding) And I adding checkboxes in the (mncTable) gridview,

  MncTable.getMncList().forEach(mncEntity -> {
            ItemCheckboxBinding checkboxBinding = ItemCheckboxBinding.inflate(inflater);
            checkboxBinding.getRoot().setText(String.format("%s-%s",mncEntity.getMnc(),mncEntity.getBrand()));
        
            filterManagerBinding.mncTable.addView(checkboxBinding.getRoot());
        });

I tried to this code below it work but it's not solution to me because of some checkbox is selected at initial (default) So I didn't get the checkbox value that was selected from the default.

checkboxBinding.getRoot().setOnCheckedChangeListener((compoundButton, isChecked) -> {
               //do something get value
});
1 Answers

I found solution to my question

 private ArrayList<CheckBox> checkBoxes = new ArrayList<>();

 MncTable.getMncList().forEach(mncEntity -> {
            ItemCheckboxBinding checkboxBinding = ItemCheckboxBinding.inflate(inflater);
            checkboxBinding.getRoot().setText(String.format("%s-%s",mncEntity.getMnc(),mncEntity.getBrand()));
        
            checkBoxes.add(checkboxBinding.getRoot());
            filterManagerBinding.mncTable.addView(checkboxBinding.getRoot());
        });

And get the value from Checkbox view added ArrayList.

checkBoxes.forEach(checkBox -> {
            if(checkBox.isChecked()) {
                Log.d(TAG, "CheckBox:" + checkBox.getText());
            }
        });
Related