Reputation: 1
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
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
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