I have a checkbox like this:
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);
}
and I want to get checked data, I do this :
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();
}
}
});
but the captured data is only at index 0. Other than that, the data is not stored. And sometimes the data is not stored at all.
Aucun commentaire:
Enregistrer un commentaire