【问题标题】:how to store the selected checkbox value in an array list in android studio?如何将选中的复选框值存储在 android studio 的数组列表中?
【发布时间】:2020-05-15 16:09:15
【问题描述】:
我在警报对话框(android studio)中有复选框选项。我希望将选中的值存储在数组列表中,以便我可以在下一个活动中使用这些值。那么我如何为相同的创建一个数组列表并将选定的值存储在其中。
【问题讨论】:
标签:
android
arraylist
checkbox
storing-information
【解决方案1】:
您可以使用以下方法。
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);
}
}
});
【解决方案2】:
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();
}
}
}
});