Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8971219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:54:23+00:00 2026-06-15T17:54:23+00:00

The output of the showResults() method in the Results.java class is not working properly.

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:54:24+00:00Added an answer on June 15, 2026 at 5:54 pm

    the variables “correctAnswers” and “wrongAnswers” are not incrementing in the QuestionView class. This is seen by the output by the showResults() in Results.java.

    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:

    public void endQuiz() {
        Intent intent = new Intent(QuestionView.this, Results.class);
        intent.putExtras("correct", correctAnswers);
        intent.putExtras("wrong", wrongAnswers);
        intent.putExtras("queries", queries); // You will need to make Question implement Parcelable 
        startActivity(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:

    OnClickListener answerClick = new OnClickListener() {
        public void onClick(View v) {
            int index = (Integer) v.getTag();
            if(answer == index) {
                correctAnswers++;
                queries.get(i).setSelectedAnswer(index);
                queries.get(i).setCorrectness(true);
            } else {
                wrongAnswers++;
                queries.get(i).setCorrectness(false);
            }
            nextQuestion();
        }
    };
    
    answer1 = (Button)findViewById(R.id.answer1);
    answer1.setOnClickListener(answerClick);
    answer1.setTag(0);
    
    answer2 = (Button)findViewById(R.id.answer2);
    answer2.setOnClickListener(answerClick);
    answer2.setTag(1);
    
    //etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

output cache is implemented in ASP.NET MVC2 using code below. GetVaryByCustomString method is not
My output is working perfect with one exception: some of the people do not
OUTPUT Fun1 is not fun 9 CODE #include <stdio.h> int fun1(void){ printf(Fun1 is not
Output: Warning: mysql_query(): 6 is not a valid MySQL-Link resource in C:...\mysql_helper.php on line
Code output needed: Visual Output : My sql Database 'test' CREATE TABLE IF NOT
My output is not as I expected, what i want is eliminated duplicated data.
Getting output with Java is pretty easy as you can make use of System.out.print
I am having trouble to get all rows data from sql. DatabaseHandler.java public HashMap<String,
Output 1 2 null 2 Code class Program { static void Main(String[] args) {
Output says all array values are anxiety when the words of file fileIn are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.