0

I am implementing a quiz and here am having a method for my button as

public  void playquiz(final int arrayIndex) {

setContentView(R.layout.quiz);

next = (Button) findViewById(R.id.nextBtn);

next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) {

                    int totalpoints = correctAnswerCount*10;
                    Intent scoreintent = new Intent(TriviaQuiz.this,ScoreBoard.class);
                    startActivity(scoreintent);
            }   
            else
            {
                   playquiz(arrayIndex+1);
            }

}

What I am trying to do is, inside the method I am loading another layout and assigning an onclick for the button in that layout.

Now my problem is, the arrayIndex which I get initially, I have to update this on click of the next button and based on this I have some other conditions to check.

But if I do like playquiz(arrayIndex+1);, it asks me to declare the arrayIndex as final, why is this?

And even then it is not behaving in the exact way as it supposed to be.

The if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) inside onClick is not happening

Any suggestion?

1
  • Why declare variable as final, check here stackoverflow.com/questions/1299837/… , because you are using inner class. and trying to access variable in outer class. Commented Sep 14, 2011 at 5:20

1 Answer 1

1
But if I do like playquiz(arrayIndex+1);, it asks me to declare the arrayIndex as final, why is this?

This is because you are using it inside another class - OnClickListener() and arrayIndex is probably a local variable. There are two ways to getting around this.

  1. declare it in the global declaration area.
  2. your class should implement OnClickListener and override OnClick method within which you must include your codes.

    The if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) inside onClick is not happening
    

I am not sure if you have provided enough code here but by just looking at it i cannot see arrayIndex being incremented and hence it will never get to TourDescription.currentTour.getTriviaArray().size(). You need to increment arrayIndex it in the else clause.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I declared a global variable and I am able to it now. The if is also working now, I declared a separate count for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.