Next/Prev buttons challenge 2


#1

I’m in the process of implementing the prev button. I implemented it almost exactly as the book told us to with the “next” button (except i decrement the index and check for out of bounds rather than increment). Both buttons work as they should, except the functionality for each is swapped. Meaning: the next button acts as the prev button and vice versa (if you press next, it goes to the previous question. if you press previous, it goes to the next question). Can anyone explain why this is happening and provide some insight on how to fix it?

[code]package com.bignerdranch.android.geoquiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {

private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mPrevButton;
private TextView mQuestionTextView;

private void updateQuestion(){
	int question = mQuestionBank[mCurrentIndex].getQuestion();
	mQuestionTextView.setText(question);
}

private void checkAnswer(boolean userPressedTrue){
	boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
	int messageResId = 0;
	if(userPressedTrue == answerIsTrue)
		messageResId = R.string.correct_toast;
	else
		messageResId = R.string.incorrect_toast;
	Toast.makeText(this,  messageResId,  Toast.LENGTH_SHORT).show();
}

private TrueFalse[] mQuestionBank = new TrueFalse[] {
		new TrueFalse(R.string.question_oceans, true),
		new TrueFalse(R.string.question_mideast, false),
		new TrueFalse(R.string.question_africa, false),
		new TrueFalse(R.string.question_americas, true),
		new TrueFalse(R.string.question_asia, true),
};

private int mCurrentIndex = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_quiz);
	
	mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
	mQuestionTextView.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
			updateQuestion();				
		}
	});
	
	int question = mQuestionBank[mCurrentIndex].getQuestion();
	mQuestionTextView.setText(question);
	
	mTrueButton = (Button)findViewById(R.id.true_button);
	mTrueButton.setOnClickListener(new View.OnClickListener() {			
		@Override
		public void onClick(View v) {
			checkAnswer(true);
		}
	});
	
	mFalseButton = (Button)findViewById(R.id.false_button);
	mFalseButton.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			checkAnswer(false);
		}
	});
	
	mNextButton = (Button)findViewById(R.id.next_button);
	mNextButton.setOnClickListener(new View.OnClickListener() {			
		@Override
		public void onClick(View v) {
			mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
			updateQuestion();
			
		}
	});
	
	mPrevButton = (Button)findViewById(R.id.prev_button);
	mPrevButton.setOnClickListener(new View.OnClickListener() {			
		@Override
		public void onClick(View v) {
			mCurrentIndex = (mCurrentIndex == 0) ? mQuestionBank.length-1 : mCurrentIndex -1;
			updateQuestion();
			
		}
	});
	
	
	updateQuestion();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.quiz, menu);
	return true;
}

}

[/code]

[code]

<TextView
    android:id="@+id/question_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    
    <Button
        android:id="@+id/true_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/true_button" />
    
    <Button 
        android:id="@+id/false_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/false_button" />
    
   </LinearLayout>
   
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    
    <Button
	    android:id="@+id/prev_button"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="@string/prev_button"
	    android:drawableLeft="@drawable/arrow_left"
	    android:drawablePadding="4dp" />

	<Button
	    android:id="@+id/next_button"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="@string/next_button" 
		android:drawableRight="@drawable/arrow_right"
		android:drawablePadding="4dp" />
	
	
	</LinearLayout>

[/code]


#2

You should try to log the index.

      mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length; 
				Log.i("IndexClick", String.valueOf(mCurrentIndex));

#3

I had the same problem today. I think it happened because R.java did not regenerate after each of my edits to activity_quiz.xml.

Try regenerating R.java. Resolve all issues in the Problems view and choose Project->Clean from the menu.

The answers on this page have more tips for regenerating R.java:
stackoverflow.com/questions/2757 … generating

EDIT:
Look ahead to Chapter 4: Issues with the R class, pp. 87-88.


#4

Going to Project->clean solved the issue for me.


#5

I believe it’s bad form on a forum such as this to pile on with “worked for me too” but I can’t find a “solved” button, as there is on Stackoverflow… So there you go. Worked for me too.