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

  • SEARCH
  • Home
  • 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 8942669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:34:18+00:00 2026-06-15T11:34:18+00:00

The program works and comes up. When I click start on the main menu

  • 0

The program works and comes up. When I click start on the main menu it brings up a question (textview) and 4 answers (buttons). Text is assigned to the textview and the 4 buttons with the for-loop. It is never looping through after clicking a button. Nothing happens when clicking a button.

I have tried putting in a “break” and changing to a while loop and just can’t get it working. I think this is a simple fix that I just cannot find.

Any other code structure or advice is appreciated and needed!

public class QuestionView extends Activity {

    Quiz quiz = new Quiz();
    ArrayList<Question> queries = quiz.getRandom10();

    int correctAnswers = 0;
    int wrongAnswers = 0;

    int answer = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questionviewmain);

        TextView question = (TextView)findViewById(R.id.question);

        Button answer1 = (Button)findViewById(R.id.answer1);
        Button answer2 = (Button)findViewById(R.id.answer2);
        Button answer3 = (Button)findViewById(R.id.answer3);
        Button answer4 = (Button)findViewById(R.id.answer4);

        for(int i = 0; i < 10; i++) {
            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());

            answer = queries.get(i).getCorrectAnswer();

            answer1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 0) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                }
            });

            answer2.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 1) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                }
            });

            answer3.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 2) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                }
            });

            answer4.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 3) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                }
            });
        }
    }
}
  • 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-15T11:34:19+00:00Added an answer on June 15, 2026 at 11:34 am

    The onCreate method is running only when the Activity is created. What you want to do is break out the following code into a separate method and call that method from the onClickLiestener() for the different buttons:

    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());
    

    Since this solution wouldn’t include a loop, you would have to make sure that the index (in this case ‘i’) is increased for every click.

    EDIT: Suggested solution

    public class QuestionView extends Activity {
    
        Quiz quiz = new Quiz();
        ArrayList<Question> queries = quiz.getRandom10();
    
        int correctAnswers = 0;
        int wrongAnswers = 0;
    
        int answer = 0;
    
        int i=0;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.questionviewmain);
    
            Button answer1 = (Button)findViewById(R.id.answer1);
            Button answer2 = (Button)findViewById(R.id.answer2);
            Button answer3 = (Button)findViewById(R.id.answer3);
            Button answer4 = (Button)findViewById(R.id.answer4);
    
            answer1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 0) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                    reloadQuestion();
                }
            });
    
            answer2.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 1) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                    reloadQuestion();
                }
            });
    
            answer3.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 2) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                    reloadQuestion();
                }
            });
    
            answer4.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if(answer == 3) {
                        correctAnswers++;
                    } else {
                        wrongAnswers++;
                    }
                    reloadQuestion();
                }
            });
    
            reloadQuestion();
        }
    
    
        private void reloadQuestion(){
            TextView question = (TextView)findViewById(R.id.question);
    
            Button answer1 = (Button)findViewById(R.id.answer1);
            Button answer2 = (Button)findViewById(R.id.answer2);
            Button answer3 = (Button)findViewById(R.id.answer3);
            Button answer4 = (Button)findViewById(R.id.answer4);
    
            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());
    
            answer = queries.get(i).getCorrectAnswer();
    
            i++;
        }
    }
    

    You could probably optimize the code in different ways, but this suggestion should solve your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The sample program that comes with the Extension Library works just fine, but when
My program works right in the commandline, but when I run it as a
I updated the way my program works with fines and charges and now I
I want to put some extra info on how program works, if theres an
I have a program that works with a variety of files on both the
I have a program that works with multiple threads. For each I set uncaught
This is my sample twitter program it works but there are several errors if
I've written a C program that works when I pipe data into my program
Problem: Have made a small mail program which works perfectly on my developer pc
I have a popup function in a program that works well, be re-loads the

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.