Previous Button Challenge



mPreviousButton = (Button) findViewById(;
mPreviousButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (mCurrentIndex == 0) {
}else {
mCurrentIndex = (mCurrentIndex -1 ) % mQuestionsBank.length;



Hi!, I’m Chinese student. Maybe my English is poor,but I want to help you.
This is my code

if(mCurrentIndex == 0)
        mCurrentIndex= mQuestionBank.length - 1;
    mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;



Thank you for your help, I appreciate it.


another example of a solution:
mCurrentIndex = (mQuestionBank.length + mCurrentIndex - 1) %mQuestionBank.length;


hi,I’m Chinese student. Maybe my English is poor,but I want to help you.And I have a another idea.
mPrevButton = (Button) findViewById(;
mPrevButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
mCurrentIndex = (mCurrentIndex + 4) % mQuestionBank.length;


I choose to +4 .As you can see, the code is short,but it can work well.


Great solution!

For anyone confused, the issue with this code:

mCurrentIndex = (mCurrentIndex - 1) %mQuestionBank.length;

is that if mCurrentIndex starts out as 0 the value in the parenthesis go to -1 and then the mod calculation throws an error and ends the program.


I also like this solution. It is essentially the same as marfars but is better because it allows you to also change the size of the array of questions.


I have a question for this part…Why we just use to (mCurrentIndex +1)??
why we must to use this mQuestionBank.length?