Reputation: 569
I'm trying to set up a quiz of which each answer will add a certain number of points to a total score. At the moment, I'm concerned with radio buttons. I have set up a radiogroup and I wish for whatever Radio button selected to add to the total score. This is only one part of my program just to note.
When I press the button at the bottom of the xml layout file, I want the score associated with the radio button to be added to the total score. Do you understand?
Here's the class file for the actual xml layout file:
Public class QuestionOne extends Results {
RadioButton answer1, answer2, answer3;
Button oneNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
RadioButton answer1 = (RadioButton) findViewById(R.id.oneradio1);
RadioButton answer2 = (RadioButton) findViewById(R.id.oneradio2);
RadioButton answer3 = (RadioButton) findViewById(R.id.oneradio3);
Button oneNext = (Button) findViewById(R.id.onenext);
}
}
It extends the Results class and I did this so it inherits the score integer. Here is the score class:
public class Results extends Activity {
private int score;
public int getScore() {
return score;
}
public Results(int score) {
this.score = score;
}
}
I got that structure from googling and its validity is questionable I'm sure but I think I have the basic idea. Can anyone help me out?
Upvotes: 0
Views: 1903
Reputation: 16354
Declare a RadioGroup instead of declaring individual RadioButtons.
answerGroup = (RadioGroup) findViewById(R.id.radioGroupAnswer);
You can get the index of the button which was selected in the RadioGroup.
int index = answerGroup.indexOfChild(findViewById(answerGroup.getCheckedRadioButtonId()));
Then you can use this index value and add score to the total score.
switch(index)
{
case 0 : totalScore = totalScore + x; // x is the score of the first answer
break;
case 1 : totalScore = totalScore + y; // y is the score of the second answer
break;
case 2 : totalScore = totalScore + z; // z is the score of the third answer
break;
}
Upvotes: 1