Shriya S Nair
Shriya S Nair

Reputation: 1

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

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.

Upvotes: -2

Views: 984

Answers (2)

Jay Kikani
Jay Kikani

Reputation: 149

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);
    }
}
});

Upvotes: 3

ashok
ashok

Reputation: 429

       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();
                   }
              }
           }
       });

Upvotes: 0

Related Questions