【问题标题】: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();
                       }
                  }
               }
           });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      相关资源
      最近更新 更多