我想根据无线电组的checkedId在ArrayList“arr”中添加数据。我有4个单选按钮“a0,a1,a2,a3”。因此,如果我选择a1,则数组列表应添加rb2的值。选择a1之后,如果我选择a2,那么“arr”中的前一个值应该更新(不会在第一个之后添加)等等。任何帮助..
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
ArrayList<String> arr = new ArrayList<String>();
for (int i = 0; i <= mcq.size(); i++) {
switch (checkedId) {
case R.id.a0:
// do operations specific to this selection
Toast.makeText(getApplication(),
rb1.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.a1:
// do operations specific to this selection
Toast.makeText(getApplication(),
rb2.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.a2:
// do operations specific to this selection
Toast.makeText(getApplication(),
rb3.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.a3:
// do operations specific to this selection
Toast.makeText(getApplication(),
rb4.getText(), Toast.LENGTH_SHORT).show();
break;
}
arr.add(String.valueOf(checkedId));
Log.e("", String.valueOf(arr));
}
}
});
答案 0 :(得分:0)
您可以使用https://developer.android.com/reference/android/util/SparseArray.html
SparaseArray<Boolean> array = new SparseArray();
array.put(checkedId, rb.isChecked());
然后你可以像这样得到数组中所有已检查的元素:
for(int i = 0; i< sparseArray.size(); i++) {
int id = sparseArray.keyAt(i);
Boolean isChecked = sparseArray.get(id);
if(isChecked) {
log("Identifier " + id + " is checked");
}
}