I'm trying to create a random multiple choice quiz for android. I want to display a random question from a stringarray, with the corresponding answer from another string array showing up in one of the four options. The other three options will come from another string array, which will be used provide the "wrong" answers for all the questions, randomly.
Two questions: Is there a better way to make a multiple choice quiz like this? -and- When the player selects an answer, how do I identify which array the answer came from?
This is the code I'm using to randomize:
String[] question = { //questions here// };
ArrayList<String> questionList = new ArrayList(Arrays.asList(question));
String[] answer = { //answers here// };
ArrayList<String> answerList = new ArrayList(Arrays.asList(answer));
String[] distractor = { //distractors here// };
ArrayList<String> distractorList = new ArrayList(Arrays.asList(distractor));
int i = 0;
Random r = new Random();
public void randomize() {
TextView word = (TextView) findViewById(R.id.textView1);
TextView choice1 = (TextView) findViewById(R.id.textView2);
TextView choice2 = (TextView) findViewById(R.id.textView3);
TextView choice3 = (TextView) findViewById(R.id.textView4);
TextView choice4 = (TextView) findViewById(R.id.textView5);
if (i < question.length) {
int remaining = r.nextInt(questionList.size());
String q = questionList.get(remaining);
word.setText(q);
questionList.remove(remaining);
String a = answerList.get(remaining);
int slot = r.nextInt(4);
TextView[] tvArray = { choice1, choice2, choice3, choice4 };
tvArray[slot].setText(a);
answerList.remove(remaining);
//an if/else statement here to fill the remaining slots with distractors
I suggest creating a new class called QuestionAndAnswer. The class should hold the question and the correct answer, it could also hold any customized wrong answers and the user's choice. The exact implementation is entirely up to you.
In your Activity have an Array of this QuestionAndAnswer class to cycle through the list asking the questions and tally up the points when done.
(I could be more specific if you include the relevant code of what you have tried.)
Addition
This is what I would start with:
(From your code I'm guessing the
distractorList
contains the false answers that you want to display.)For the Activity I changed your four answer TextViews into a RadioGroup, this way the user can intuitively select an answer. I also assume that there will be
prev
andnext
buttons, they will adjustint currentQuestion
and callfillInQuestion()
.You may have noticed that QuestionAndAnswer has an isCorrect() method, when it is time to grade the quiz you can count the correct answers like this:
This is my general idea. The code is a complete thought, so it will compile. Of course, you'll want to add a "next" Button to see the different questions. But this is enough for you to see one way to randomize your questions and answers while keeping them organized.
Here a sample, you can try. This is the data-model like to hold stuffs for the Question-Answer thing.
Ok, so you everytime you display a question, you got all options to answer, and the correct answer of each question. Just create a proper data structure to hold them. Anyway, just a sample, not a perfect one, but give it a try if you're new on this stuffs ^^!