Matt
Matt

Reputation: 3912

Not looping through for loop - Java (android)

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++;
                    }
                }
            });
        }
    }
}

Upvotes: 0

Views: 361

Answers (2)

avepr
avepr

Reputation: 1896

What your program does is just ten times assign different texts from "queries" ArrayList to the bunch of TextView objects. Also it assigns the same onClickListener objects to the same textView objects over and over again. And it all happens when the activity is created. So at the moment when you are actually able to see your activity, the loop is over and all the textView items have the last (10th) values. When you click the button, the program does exactly what you have asked it to do: increases one of the counters.

I believe this is not what you have in your mind. And what you really want to do is:

  1. Make only one onClickListener object and assign it in the onCreate() method to all your textView items.
  2. Inside the onClickListener check the view that was clicked (determine which textVIew was clicked) and compare it with the correct answer. Update counters accordingly. Read next set of question-answer data and update the UI.

Here is an example of what I am talking about:

public class QuestionView extends Activity {

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

    private int correctAnswers;
    private int wrongAnswers;
    private char mCurrentAnswer;
    private char mNextQuestion;

    private View.OnClickListener clickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.getId() == getCurrentAnswer()) {
                ++correctAnswers;
            } else {
                ++wrongAnswers;
            }

            readNextQuestion();
        }
    }; 

    private int getCurrentAnswer() {
        return mCurrentAnswer;
    }

    private void readNextQuestion() {
        question.setText(queries.get(mNextQuestion).getQuery());
        answer1.setText(queries.get(mNextQuestion).getA1());
        answer2.setText(queries.get(mNextQuestion).getA2());
        answer3.setText(queries.get(mNextQuestion).getA3());
        answer4.setText(queries.get(mNextQuestion).getA4());

        mCurrentAnswer = queries.get(mNextQuestion).getCorrectAnswer();

        ++mNextQuestion;
    }

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

        //You can also assign it in the xml (I guess)
        answer1.setId(1);
        answer1.setOnClickListener(clickListener);
        answer2.setId(2);
        answer2.setOnClickListener(clickListener);
        answer3.setId(3);
        answer3.setOnClickListener(clickListener);
        answer4.setId(4);
        answer4.setOnClickListener(clickListener);

        readNextQuestion();
    }
}

Upvotes: 1

gabga
gabga

Reputation: 178

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.

Upvotes: 1

Related Questions