0

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:

5
  • What would happen if the user presses same checkbox again then? then, in that case, you would have to remove that value String array? Commented Oct 19, 2019 at 10:04
  • Kishan Maurya ai have no idea about how to handel this problem. Is ArrrayList is better option. Sir plz give me solution the code....tnxs in advance Commented Oct 19, 2019 at 10:16
  • Please let me know if your problem is solved Commented Oct 19, 2019 at 11:09
  • I hope it'll help you check it out: stackoverflow.com/a/36597344 Commented Oct 19, 2019 at 11:11
  • Kishan Maurya thank you so much its working for me....... Commented Oct 19, 2019 at 16:02

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.