I made some questions in one activity and every answer has a point(0 to 3). For example if the user select the first one, he gets 0 point, if he select the secound one he will get 1 point etc(2 point for the third one and 3 point for the forth).
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.first_radiobutton) {
totalPoint = totalPoint + 0;
} else if (checkedId == R.id.secound_radiobutton) {
totalPoint = totalPoint + 1;
}else if (checkedId == R.id.third_radiobutton) {
totalPoint = totalPoint + 2;
}else if (checkedId == R.id.forth_radiobutton) {
totalPoint = totalPoint + 3;
}
}
});
I made totalPoint
as global variable and initialize it as public static int totalPoint = 0;
. The problem is that if the user has changed his mind and select another answer for the current question, the totalPoint
variable will add the point to itself. For example imagine that if the user click on the third radioButton
, the totalPoint
variable will be 2, so if the user has changed his mind and select the forth radioButton
, the totalPoint
must be 3 but instead it will be 5(2+3). How can I avoid this?