我建议创建一个名为 QuestionAndAnswer 的新类。该类应该保存问题和正确答案,它还可以保存任何自定义的错误答案和用户的选择。确切的实施完全取决于您。
在您的 Activity 中有一个此 QuestionAndAnswer 类的数组,以循环浏览列表,询问问题并在完成后计算分数。
(如果您包含您尝试过的相关代码,我可能会更具体。)
添加
这就是我要开始的内容:(
从您的代码中,我猜测其中distractorList
包含您要显示的错误答案。)
public class QuestionAndAnswer {
public List<String> allAnswers; // distractors plus real answer
public String answer;
public String question;
public String selectedAnswer;
public int selectedId = -1;
public QuestionAndAnswer(String question, String answer, List<String> distractors) {
this.question = question;
this.answer = answer;
allAnswers = new ArrayList<String> (distractors);
// Add real answer to false answers and shuffle them around
allAnswers.add(answer);
Collections.shuffle(allAnswers);
}
public boolean isCorrect() {
return answer.equals(selectedAnswer);
}
}
对于 Activity,我将您的四个答案 TextViews 更改为 RadioGroup,这样用户可以直观地选择答案。我还假设会有prev
和next
按钮,它们会调整int currentQuestion
和调用fillInQuestion()
。
public class Example extends Activity {
RadioGroup answerRadioGroup;
int currentQuestion = 0;
TextView questionTextView;
List<QuestionAndAnswer> quiz = new ArrayList<QuestionAndAnswer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
questionTextView = (TextView) findViewById(R.id.question);
answerRadioGroup = (RadioGroup) findViewById(R.id.answers);
// Setup a listener to save chosen answer
answerRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId > -1) {
QuestionAndAnswer qna = quiz.get(currentQuestion);
qna.selectedAnswer = ((RadioButton) group.findViewById(checkedId)).getText().toString();
qna.selectedId = checkedId;
}
}
});
String[] question = { //questions here// };
String[] answer = { //answers here// };
String[] distractor = { //distractors here// };
ArrayList<String> distractorList = Arrays.asList(distractor);
/* I assumed that there are 3 distractors per question and that they are organized in distractorList like so:
* "q1 distractor 1", "q1 distractor 2", "q1 distractor 3",
* "q2 distractor 1", "q2 distractor 2", "q2 distractor 3",
* etc
*
* If the question is: "The color of the sky", you'd see distractors:
* "red", "green", "violet"
*/
int length = question.length;
for(int i = 0; i < length; i++)
quiz.add(new QuestionAndAnswer(question[i], answer[i], distractorList.subList(i * 3, (i + 1) * 3)));
Collections.shuffle(quiz);
fillInQuestion();
}
public void fillInQuestion() {
QuestionAndAnswer qna = quiz.get(currentQuestion);
questionTextView.setText(qna.question);
// Set all of the answers in the RadioButtons
int count = answerRadioGroup.getChildCount();
for(int i = 0; i < count; i++)
((RadioButton) answerRadioGroup.getChildAt(i)).setText(qna.allAnswers.get(i));
// Restore selected answer if exists otherwise clear previous question's choice
if(qna.selectedId > -1)
answerRadioGroup.check(qna.selectedId);
else
answerRadioGroup.clearCheck();
}
}
您可能已经注意到 QuestionAndAnswer 有一个 isCorrect() 方法,当需要给测验评分时,您可以像这样计算正确答案:
int correct = 0;
for(QuestionAndAnswer question : quiz)
if(question.isCorrect())
correct++;
这是我的一般想法。代码是一个完整的想法,所以它会编译。当然,您需要添加一个“下一步”按钮来查看不同的问题。但这足以让您看到一种在保持问题和答案井井有条的同时随机化问题和答案的方法。