About Previous Button


#1

Hi everyone,

I keep getting the “GeoQuiz has stopped” message everytime I try to click on the Prev Button (beeing at index = 0), so I never catch the last “question/answer” of the mQuestionBank array. Does anybody know the algorithm to loop arround the arrays values backwards? BTW here is my code:

    mPrevButton = (Button)findViewById(R.id.previous_button);
    mPrevButton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if(mCurrentIndex!=0){

            mCurrentIndex=(mCurrentIndex-1);


            }
            else{

                mCurrentIndex= mQuestionBank.length;

            }
            updateQuestion();
        }
    });

#2

This is the code I used - I think it works OK, I’m not sure I fully tested it:

        //Challenge - Add a Previous Button
        mPrevButton = (Button) findViewById(R.id.prev_button);
        mPrevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = mCurrentIndex - 1;
                if (mCurrentIndex < 0) { // wrap back
                    mCurrentIndex = mQuestionBank.length - 1;
                }
                updateQuestion();
            }
        });

#3

Look like in your “if” statement that when the mCurrentIndex is already zero it would be go negative if you press the press the previous button. Seem like android doesn’t allow that so it crashes. To make it loop back maybe you could tell it to make mCurrentIndex to be the the number of questions total (mQuestionBank.length??) but only when you press the previous button and the index is already 0

Here’s what I used:

mPrevButton=(ImageButton) findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentIndex > 0) {
mCurrentIndex = (mCurrentIndex - 1);
updateQuestion();
} else
mCurrentIndex= (mQuestionBank.length-1);
updateQuestion();
}});


#4
mCurrentIndex = (mCurrentIndex + mQuestionBank.length - 1) % mQuestionBank.length;

one line code FYI


#5

The answers are incorrect. Who knows the right answer?


#6

I just figured out 1 way to the correct answer:

    mPrevButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mCurrentIndex > 0) {
                mCurrentIndex = (mCurrentIndex - 1);
            }
             else {
                mCurrentIndex = (mQuestionBank.length - 1);
            }
            updateQuestion();
        }
    });

#7

The left arrow icon is located at the right side of my PrevButton - how can I get it to the left side?


#8

Nevermind, I figured it out: android:drawableLeft="@drawable/arrow_left".