Farooq Khan
Farooq Khan

Reputation: 21

Store Selected CheckBox values in String array

I have one Custom dialog box, which contanis one Recylerview + SearchView of CarBodyColorsNames. And one more thing I have adapter that have a custom row for each item. custom row consist of one Imagview(icons), TextView(colorName) and Checkbox for Selection.Its working fine but the problem is that, I want to store all the values that user has checked in String array according to its adapter position. following is my code:

edBodyColorAdapter.java

holder.checkBoxColor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {


                if(isChecked)
                {
                    int position = holder.getAdapterPosition();
                    clickedColorNamePosition = edBodyColorArrayList.indexOf(filteredArrayList.get(position));

                    Toast.makeText(context, "current position = " + clickedColorNamePosition, Toast.LENGTH_SHORT).show();
                    String name = edBodyColorArrayList.get(clickedColorNamePosition).getBodyColorName();

                    Toast.makeText(context, "name = " + name, Toast.LENGTH_SHORT).show();


                }
                else
                {
                    Toast.makeText(context, "Unchecked", Toast.LENGTH_SHORT).show();
                }


            }
    });

Simple I want when user click on Ok button i want to pick all the selected checkbox values.

view the preview:

Upvotes: 0

Views: 836

Answers (1)

Kishan Maurya
Kishan Maurya

Reputation: 3394

Declare globally 1 Hashmap, which is used to put/remove selected/unselected value as user check/unchecked Checkbox

HashMap<Integer, String> selectionMap = new HashMap<>();

Now In your code,

holder.checkBoxColor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    int position = holder.getAdapterPosition();
                    clickedColorNamePosition = edBodyColorArrayList.indexOf(filteredArrayList.get(position));
                    String name = edBodyColorArrayList.get(clickedColorNamePosition).getBodyColorName();
                //this mthod will check if selected checkbox value is already present or not. It present then remove ( means user unchecked box) and if value is not there means user has selected checkbox
                checkAndRemove(position,name);
            }
    });

This method is used to Put or remove value from hashmap.

private void checkAndRemove(int position, String name) {
        if(selectionMap.containsKey(position)){
            selectionMap.remove(position);
        }else {
            selectionMap.put(position, name);
        }
    }

Now After user click OK button then use this hashmap to get selected values. Iterate over hashmap value set then you will get All selected values.

Upvotes: 2

Related Questions