how to store the selected checkbox value in an array list in android studio?

920 Views Asked by At

I have check box options in Alert Dialog(android studio).I want the checked value to be stored in array list so that i can use the values in the next activity.So how do i create a array list for the same and store the selected values in it.

2

There are 2 best solutions below

0
ashok On
       HashMap<String, String> ckList = new HashMap<>();

       ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               if(isChecked){
                  ckList.put(ckbox.getText().toString(),ckbox.getText().toString());
               }
              else{
                   try{
                      ckList.remove(ckbox.getText().toString());
                   }catch(NullPointerException e){
                        e.printStackTrace();
                   }
              }
           }
       });
0
Jay Kikani On

You can use with below methods.

ArrayList<String> ids = new ArrayList<>();

ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (ids.contains(yourid)){
        ids.remove(yourid);
    }else {
        ids.add(yourid);
    }
}
});