我有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