【问题标题】:How to get value of Checkbox and save in Array?如何获取复选框的值并保存在数组中?
【发布时间】:2021-09-04 13:53:50
【问题描述】:

我有一个这样的复选框:

LinearLayout layout = findViewById(R.id.lyLayout);
CheckBox checkBox;

for (int i = 0; i < items.size(); i++) {
    checkBox = new CheckBox(getBaseContext());
    checkBox.setId(View.generateViewId());
    checkBox.setText(items.get(i).getDesk());
    layout.addView(checkBox); 
}

我想获取检查数据,我这样做:

ArrayList<String> checkedBox = new ArrayList<>();
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
    for (int a = 0; a < layout.getChildCount(); a++) {
        checkBox = (CheckBox) layout.getChildAt(a);
        if (checkBox.isChecked()) {
            checkedBox.add(checkBox.getText().toString());
            Toast.makeText(getApplicationContext(), checkedBox.toString() + " checked", Toast.LENGTH_LONG).show();
        } else {
            checkedBox.remove(checkBox.getText().toString());
            Toast.makeText(getApplicationContext(), checkedBox.toString() + " checked", Toast.LENGTH_LONG).show();
        }
    }
});

但捕获的数据仅在索引 0 处。除此之外,不存储数据。有时数据根本没有存储。

【问题讨论】:

  • checkBox 仅指最后添加的复选框,因此您只是在最后一个上注册了一个侦听器。
  • 谢谢,这对我有帮助。我终于明白错误在哪里了。

标签: java android android-studio arraylist checkbox


【解决方案1】:

试试下面的代码。

 private ArrayList<String> getCheckBoxData(){
     ArrayList<String> checkedBox = new ArrayList<>();
     for (int a = 0; a < layout.getChildCount(); a++) {
         checkBox = (CheckBox) layout.getChildAt(a);
         if (checkBox.isChecked()) {
              checkedBox.add(checkBox.getText().toString());
             Toast.makeText(getApplicationContext(), checkBox.getText().toString() + " checked", Toast.LENGTH_LONG).show();
         } 
     }
 return checkedBox;
 }

【讨论】:

  • 感谢您回答我的问题。我已经尝试过您的建议,但仍然无法获取数据。我认为在将您的代码改编为我的代码时我错了。
【解决方案2】:

我已经解决了这个错误

我从这里更改了我的代码:

ArrayList<String> checkedBox = new ArrayList<>();
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> 
    {
        for (int a = 0; a < layout.getChildCount(); a++) {
        checkBox = (CheckBox) layout.getChildAt(a);
        if (checkBox.isChecked()) {
        checkedBox.add(checkBox.getText().toString());
        Toast.makeText(getApplicationContext(), checkedBox.toString() + " checked", Toast.LENGTH_LONG).show();

        } else {
            checkedBox.remove(checkBox.getText().toString());
            Toast.makeText(getApplicationContext(), checkedBox.toString() + " checked", Toast.LENGTH_LONG).show();
            }
        }
    });

到这里:

checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                                        if(isChecked){
                                            checkedBox.add(buttonView.getText().toString());
                                            Toast.makeText(getApplicationContext(), checkedBox.toString() + " checked", Toast.LENGTH_LONG).show();

                                        } else {
                                            checkedBox.remove(buttonView.getText().toString());
                                            Toast.makeText(getApplicationContext(), checkedBox.toString() + " checked", Toast.LENGTH_LONG).show();
                                        }
                                    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 2016-09-16
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多