我有5個問題組成的報告(可以有只有一個答案或多選答案)。每個報告的問題都不一樣。所以我每次生成問題和答案爲RadioButtons
(一個答案)或CheckBoxes
(多選答案)......但現在我真的不知道如何保存這些答案(我想保存爲_question_id
,_answer_id
) 。從RadioButtons和複選框獲取信息
我如何分配好_answer_id
,以_question_id
...
感謝您的幫助提前
我有5個問題組成的報告(可以有只有一個答案或多選答案)。每個報告的問題都不一樣。所以我每次生成問題和答案爲RadioButtons
(一個答案)或CheckBoxes
(多選答案)......但現在我真的不知道如何保存這些答案(我想保存爲_question_id
,_answer_id
) 。從RadioButtons和複選框獲取信息
我如何分配好_answer_id
,以_question_id
...
感謝您的幫助提前
嘗試是這樣的:
public void saveAnswers() {
LinearLayout root = (LinearLayout) findViewById(R.id.frame); //or whatever your root control is
loopQuestions(root);
}
private void loopQuestions(ViewGroup parent) {
for(int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
if(child instanceof RadioGroup) {
//Support for RadioGroups
RadioGroup radio = (RadioGroup)child;
storeAnswer(radio.getId(), radio.getCheckedRadioButtonId());
}
else if(child instanceof CheckBox) {
//Support for checkboxes
CheckBox cb = (CheckBox)child;
int answer = cb.isChecked() ? 1 : 0;
storeAnswer(cb.getId(), answer);
}
else {
//Support for other controls
}
if(child instanceof ViewGroup) {
//Nested Q&A
ViewGroup group = (ViewGroup)child;
loopQuestions(group);
}
}
}
private void storeAnswer(int question, int answer) {
//TODO: Store your answer to disk
}
我想你正在使用RadioGroup中的一組相互排斥的單選按鈕的?然後:
radioGroup.getCheckedRadioButtonId() // gets Id of clicked RadioButton within group
的複選框必須檢查每個按鈕:
checkBox.isChecked()
非常感謝,我做了某事類似的由我自己...當單擊RadioButton或選中CheckBox(當從Map中刪除信息被取消選中時),我將信息<_question_ID,_answer_ID>存儲到Map集合中...當點擊saveAnwers時,Map被保存到DB中 – 2010-11-05 08:21:58