Reputation: 13
I am trying to develop a quizz app using Listview the idea of app is having list of question each question has 4 Radio Buttons to choose the answer and after finishing all questions user click submit button the submit button move the user to Score layout with his total Score
my problem is : i can't implement the logic of Score Count i added an if condition that work everytime a change happed to RadioButtons it check the answer if it right it add +1 to the score the problem is happening if the user pick the right answer then change it to wrong answer the score keep the same and if i add counter-- when the answer is wrong the score became negative as there is multiple of wrong answers
final RadioGroup rd = (RadioGroup) convertView.findViewById(R.id.RadioGroup);
rd.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (getItem(position).getAnswer() == rd.getCheckedRadioButtonId()) {
counter++;
}
}
});
Full Project : https://github.com/engmedo800/QuizzChan6
Upvotes: 1
Views: 85
Reputation: 301
Count only correct (true) answers - recalculate all answers after each answer of user. In that case you will not have any issue.
Upvotes: 0
Reputation: 2186
I think you should have some array with booleans, which would hold response correctness for every of the questions, like so:
the array field declaration:
private boolean[] isCorrectAnswer;
the array field initialization in Adapter constructor (it's called QuestionAdaptor in your project):
public QuestionAdaptor(Context context, ArrayList<Question> questionArray) {
super(context, 0, questionArray);
isCorrectAnswer = new boolean[questionArray.size()];
}
the code for listener:
final RadioGroup rd = (RadioGroup)
convertView.findViewById(R.id.RadioGroup);
rd.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (getItem(position).getAnswer() == rd.getCheckedRadioButtonId()) {
isCorrectAnswer[position] = true;
} else {
isCorrectAnswer[position] = false;
}
});
the getCorrectAnswersCount() code:
int getCorrectAnswersCount() {
int count = 0;
for (int i = 0; i < isCorrectAnswer.length; i++) {
if (isCorrectAnswer[i]) {
count++;
}
}
return count;
}
Upvotes: 1