在我的LinearLayout中,可選數量的複選框。在一個月前的一個問題中,有人說最好是動態添加複選框,而不是讓它們不可見。LinearLayout中複選框的最佳解決方案
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()獲得一個新List。並使用它們的值在「antwortencode」列表中填入一個新字符串。
如果我回到最後的位置,我已經產生了字符串,但複選框不再被檢查。但他們從舊的位置有弦。這意味着除了複選框的狀態以外,所有內容都會保存。我不能設置一個cBox.setChecked(isChecked)或buttonView.setChecked(isChecked)或buttonView.setChecked(buttonView.isChecked())或在語法上幾乎相同的東西。
我不知道我可以做什麼,除了在xml文件中聲明10個複選框以逐個跟他們交談,並且如果auswahlList.get(position).isEmpty()設置爲VISIBLE.false。
重要提示:我的XML是一個可滾動活動,因爲內容的大小過度擴展屏幕。這就是爲什麼我沒有和不能使用ListView。所以我需要一個使用LinearLayout的解決方案
好的,但我不能使用ListView導致它擾亂了我的ScrollableActivity並被顛倒。在一項活動中使用它們是不可能的(?)。至於我讀過關於這個問題的計算器的主題。但我設法單獨解決我的問題。編程8小時後,我的頭和手指開始做垃圾。 –