Android radio button style quiz, add value to correct/incorrect answers?



I'm working on a questions/answers app in android in the style of a quiz, and I haven't been able to come up with a method to properly implement a value to radio button. The value is determined by whether the answer is correct or wrong.


I feel like there's a few ways to go about it, but I'm not sure which ones might cause issues, or are simply bad programming practices. My issue comes from the fact that the questions and answers are randomized every time the activity is restarted. If it wasn't random, and I knew which answer would be correct, I think it'd be easier, but I want to go with a random feature.


To help clarify, I have radio buttons in an XML:



<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content">


<RadioButton
android:id="@+id/radioAnswerOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<RadioButton
android:id="@+id/radioAnswerTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<RadioButton
android:id="@+id/radioAnswerThree"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<RadioButton
android:id="@+id/radioAnswerFour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>



</RadioGroup>


And I have the strings for the radio buttons in the .java file:



TextView q1A1 = (TextView) findViewById(R.id.radioAnswerOne);
q1A1.setText("This ANSWER is " + global.getA(0, 0));

TextView q1A2 = (TextView) findViewById(R.id.radioAnswerTwo);
q1A2.setText("This ANSWER is " + global.getA(0, 1));

TextView q1A3 = (TextView) findViewById(R.id.radioAnswerThree);
q1A3.setText("This ANSWER is " + global.getA(0, 2));

TextView q1A4 = (TextView) findViewById(R.id.radioAnswerFour);
q1A4.setText("This ANSWER is " + global.getA(0, 3));


The strings are pulled from a class. Each question has a class. That class has a question string, and an array of 4 answer classes. The answer class has a string and a status (true/correct or false/incorrect). I'm looking to implement the status portion now into the radio buttons to determine which one is correct so that if the user selects that radio button and clicks a button (to go to the next question), then the grade is calculated/adjusted accordingly.


I'm curious in how to do this. Seeing as the questions and answers are randomized everytime, I'll need to implement a function to get the status of each question and place that status as some sort of variable attached to the button. And whichever button is selected, that variable gets pulled and is added to an integer of "questions answered correctly" and the app continues. But I'm not sure of the java implementation to do such a thing. Should my radio buttons be in the Java file instead of XML to work best? Don't know the truly best way to do it (if there is one).


No comments:

Post a Comment