【发布时间】:2017-08-30 12:50:17
【问题描述】:
在我的 LinearLayout 中,有可变数量的 CheckBox。在我一个月前的一个问题中,有人说最好动态添加复选框而不是让它们不可见。
Integer[] count = new Integer[]{1,2,3,4,5,6,7,8,9,10};
size = mFrageList.get(position).getAuswahlList().size();
for (int i = 0; i < size; i++) {
cBox = new CheckBox(this);
cBox.setText(mFrageList.get(position).getAuswahlList().get(i));
cBox.setId(count[i]);
cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
antwortencode[position] += "" + buttonView.getId();
frageBeantworten.setText("Antwort :"+antwortencode[position]+" abgeben");
} else {
String id = Integer.toString(buttonView.getId());
antwortencode[position] = antwortencode[position].replaceAll(id,"");
if(!antwortencode[position].isEmpty() || antwortencode[position]!= "") {
frageBeantworten.setText("Antwort :" + antwortencode[position] + " abgeben");
} else {
frageBeantworten.setText("Keine Checkbox(en) gewählt");
}
}
}
});
antworten.addView(cBox);
目前,我可以使用选中的复选框保存一个字符串,如果我取消选中一个复选框,它会从字符串中删除它的值。 如果我更新活动,则保存字符串,并且复选框从 mFrageList.get(position)getAuswahlList(); 获取一个新列表;并用它们的值在“antwortencode”列表中填充一个新字符串。
如果我回到最后一个位置,我有生成的字符串,但不再选中复选框。但是他们有旧位置的字符串。这意味着除了复选框的状态之外的所有内容都已保存。我无法设置 cBox.setChecked(isChecked) 或 buttonView.setChecked(isChecked) 或 buttonView.setChecked(buttonView.isChecked()) 或语法几乎相同的东西。
我不知道除了在 xml 文件中声明 10 个复选框以与它们一一对话并在 auswahlList.get(position).isEmpty() 时设置 VISIBLE.false 之外我还能做什么。
重要提示:我的 XML 是一个可滚动的活动,因为内容的大小超出了屏幕。这就是为什么我没有也不能使用 Listview 的原因。所以我需要一个使用 LinearLayout 的解决方案
【问题讨论】: