The output of the showResults() method in the Results.java class is not working properly. The trivia game loops through all 10 questions and displays for the user to select their answers. After the 10th question, a results page is displayed with the results of the correct and incorrect answers. The questions that were correct are to be displayed in green and the questions that were incorrect are to be displayed in red. The colors of the questions are not being being displayed in the correct colors. Also, the variables “correctAnswers” and “wrongAnswers” are not incrementing in the QuestionView class. This is seen by the output by the showResults() in Results.java.
I have debugged in a lot of places but just cannot seem to find where I am messing up.
Help on this problem and also code structure advice is appreciated! Thank you in advance!
Results.java
public class Results extends Activity {
QuestionView qv = new QuestionView();
ArrayList<Question> queryList = qv.getQueries();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultsmain);
Button homeBtn = (Button)findViewById(R.id.homeBtn);
Button highscoresBtn = (Button)findViewById(R.id.highscoresBtn);
homeBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent1 = new Intent(Results.this, MainMenu.class);
startActivity(intent1);
}
});
highscoresBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent2 = new Intent(Results.this, Highscores.class);
startActivity(intent2);
}
});
showResults();
}
public void showResults() {
ArrayList<TextView> tList = new ArrayList<TextView>(10);
TextView header = (TextView)findViewById(R.id.header);
TextView q1 = (TextView)findViewById(R.id.q1);
TextView q2 = (TextView)findViewById(R.id.q2);
TextView q3 = (TextView)findViewById(R.id.q3);
TextView q4 = (TextView)findViewById(R.id.q4);
TextView q5 = (TextView)findViewById(R.id.q5);
TextView q6 = (TextView)findViewById(R.id.q6);
TextView q7 = (TextView)findViewById(R.id.q7);
TextView q8 = (TextView)findViewById(R.id.q8);
TextView q9 = (TextView)findViewById(R.id.q9);
TextView q10 = (TextView)findViewById(R.id.q10);
tList.add(q1);
tList.add(q2);
tList.add(q3);
tList.add(q4);
tList.add(q5);
tList.add(q6);
tList.add(q7);
tList.add(q8);
tList.add(q9);
tList.add(q10);
tList.get(0).setText(queryList.get(0).getQuery());
if(queryList.get(0).getCorrectness() == true) {
tList.get(1).setText("Changed to true");
} else {
tList.get(1).setText("DID NOT Change to true");
}
tList.get(2).setText(Integer.toString(qv.getCorrectAnswers()));
tList.get(3).setText(Integer.toString(qv.getWrongAnswers()));
/* for(int i = 0; i < 10; i++) {
tList.get(i).setText(queryList.get(i).getQuery());
if(queryList.get(i).getCorrectness() == true) {
tList.get(i).setTextColor(Color.GREEN);
} else {
tList.get(i).setTextColor(Color.RED);
}
}*/
}
}
QuestionView.java
public class QuestionView extends Activity {
Quiz quiz = new Quiz();
ArrayList<Question> queries = quiz.getRandom10();
int correctAnswers = 3;
int wrongAnswers = 3;
int answer = 0;
int i = 0;
Button answer1;
Button answer2;
Button answer3;
Button answer4;
TextView question;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionviewmain);
answer1 = (Button)findViewById(R.id.answer1);
answer2 = (Button)findViewById(R.id.answer2);
answer3 = (Button)findViewById(R.id.answer3);
answer4 = (Button)findViewById(R.id.answer4);
question = (TextView)findViewById(R.id.question);
loadQuestion();
}
public void loadQuestion() {
if(i == 9) {
endQuiz();
} else {
answer = queries.get(i).getCorrectAnswer();
question.setText(queries.get(i).getQuery());
answer1.setText(queries.get(i).getA1());
answer2.setText(queries.get(i).getA2());
answer3.setText(queries.get(i).getA3());
answer4.setText(queries.get(i).getA4());
answer1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 0) {
correctAnswers++;
queries.get(i).setSelectedAnswer(0);
queries.get(i).setCorrectness(true);
nextQuestion();
} else {
wrongAnswers++;
queries.get(i).setCorrectness(false);
nextQuestion();
}
}
});
answer2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 1) {
correctAnswers++;
queries.get(i).setSelectedAnswer(1);
queries.get(i).setCorrectness(true);
nextQuestion();
} else {
wrongAnswers++;
queries.get(i).setCorrectness(false);
nextQuestion();
}
}
});
answer3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 2) {
correctAnswers++;
queries.get(i).setSelectedAnswer(2);
queries.get(i).setCorrectness(true);
nextQuestion();
} else {
wrongAnswers++;
queries.get(i).setCorrectness(false);
nextQuestion();
}
}
});
answer4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 3) {
correctAnswers++;
queries.get(i).setSelectedAnswer(3);
queries.get(i).setCorrectness(true);
nextQuestion();
} else {
wrongAnswers++;
queries.get(i).setCorrectness(false);
nextQuestion();
}
}
});
}
}
public int getCorrectAnswers() { return correctAnswers; }
public int getWrongAnswers() { return wrongAnswers; }
public ArrayList<Question> getQueries() {
return queries;
}
public void nextQuestion() {
i++;
loadQuestion();
}
public void endQuiz() {
Intent intent = new Intent(QuestionView.this, Results.class);
startActivity(intent);
}
}
Quiz.java
public class Quiz {
ArrayList<Question> qList = new ArrayList<Question>(100);
ArrayList<Question> tenQs = new ArrayList<Question>(10);
public Quiz() {
qList.add(new Question("A", "B", "C", "D", 3, "Question 1?", 0, false));
qList.add(new Question("A", "B", "C", "D", 3, "Question 2?", 0, false));
qList.add(new Question("A", "B", "C", "D", 3, "Question 3?", 0, false));
qList.add(new Question("A", "B", "C", "D", 3, "Question 4?", 0, false));
qList.add(new Question("A", "B", "C", "D", 3, "Question 5?", 0, false));
qList.add(new Question("A", "B", "C", "D", 3, "Question 6?", 0, false));
qList.add(new Question("A", "B", "C", "D", 3, "Question 7?", 0, false));
qList.add(new Question("A", "B", "C", "D", 3, "Question 8?", 0, false));
qList.add(new Question("A", "B", "C", "D", 3, "Question 9?", 0, false));
qList.add(new Question("A", "B", "C", "D", 3, "Question 10?", 0, true));
qList.add(new Question("A", "B", "C", "D", 3, "Question 11?", 0, true));
qList.add(new Question("A", "B", "C", "D", 3, "Question 12?", 0, true));
qList.add(new Question("A", "B", "C", "D", 3, "Question 13?", 0, true));
qList.add(new Question("A", "B", "C", "D", 3, "Question 14?", 0, true));
}
public ArrayList<Question> getRandom10() {
Random r = new Random();
for(int i = 0; i < 10; i++) {
Question x = qList.get(r.nextInt(qList.size()));
tenQs.add(x);
}
return tenQs;
}
}
Question.java
public class Question {
String a1;
String a2;
String a3;
String a4;
int correctAnswer;
String query;
int selectedAnswer;
boolean correctness;
public Question() {
}
public Question(String a1, String a2, String a3, String a4, int correctAnswer, String query, int selectedAnswer, boolean correctness) {
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
this.a4 = a4;
this.correctAnswer = correctAnswer;
this.query = query;
this.selectedAnswer = selectedAnswer;
this.correctness = correctness;
}
public String getA1() { return a1; }
public String getA2() { return a2; }
public String getA3() { return a3; }
public String getA4() { return a4; }
public String getQuery() { return query; }
public int getCorrectAnswer() { return correctAnswer; }
public boolean getCorrectness() { return correctness; }
public void setSelectedAnswer(int newAnswer) {
selectedAnswer = newAnswer;
}
public void setCorrectness(boolean newAnswer) {
correctness = newAnswer;
}
}
Once you put an Activity in the background there is no guarantee that it will not be destroyed without warning. You need to pass the answer counts in your Intent:
Then read these values in your result Activity. Otherwise you run the risk losing this information and getting the default value of 3. (In short there is nothing wrong with
correctAnswers++.)Also you have a lot of very similar / duplicate code. For instance you can use one OnClickListener on your answers with some minor changes: