Always displaying false


#1

Hi, I have made a few modifications to the code while following the challenges. I finished all the challenges but for the life of me I can’t figure out why it always displays false when you decide to cheat. I debugged and it shows as executing “mAnswerTextView.setText(R.id.true_button);” which should show true but on the screen it shows false. here is my cheatActivity code:

[code]package com.biancamigueis.quizzy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends Activity {

public static final String EXTRA_ANSWER_IS_TRUE = "com.biancamigueis.quizzy.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.biancamigueis.quizzy.answer_shown";
private static final String IS_CHEATER = "false";
private static final String TAG = "CheatActivity";

private TextView mAnswerTextView;
private Button mShowAnswer;

private boolean mAnswerIsTrue;
private boolean mCheater;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_cheat);
	
	mCheater = false;
	if (savedInstanceState != null) {
		mCheater = savedInstanceState.getBoolean(IS_CHEATER, false);
	}
	mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
	
	mAnswerTextView = (TextView) findViewById(R.id.andwerTextView);		
	mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
	
	setAnswerShownResult(mCheater);
	
	if (mCheater == true) {
		showAnswer(mAnswerIsTrue);	
	}
	
	mShowAnswer.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			showAnswer(mAnswerIsTrue);	
			setAnswerShownResult(true);
		}
		
	});
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
	super.onSaveInstanceState(savedInstanceState);
	Log.i(TAG, "onSaveInstanceState");
	savedInstanceState.putBoolean(IS_CHEATER, mCheater);
}

private void setAnswerShownResult (boolean isAnswerShown) {
	Intent data = new Intent();
	data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
	setResult(RESULT_OK, data);
	mCheater = isAnswerShown;
}

private void showAnswer(boolean mAnswer) {
	if (mAnswer) {
		mAnswerTextView.setText(R.id.true_button);
	} else {
		mAnswerTextView.setText(R.id.false_button);
	}
}

}
[/code]


#2

Try using R.string instead of R.id.


#3

Hi,
new member here. First of all let me say the book is great. So far so good. :wink:
First question: I was experiencing the same behaviour of the topic starter, having always returned false when onClick(View v) is called by mShowAnswer.

Changing R.id.true_button to R.string.true_button (and the same for the false case) fixes it.

Could you kindly explain why?
Thanks


#4

[quote=“Marcos”]
Changing R.id.true_button to R.string.true_button (and the same for the false case) fixes it.

Could you kindly explain why?
Thanks[/quote]

I am not the author, but it seems to me that
R.id.true_button is the button itself
while
R.string.true_button is the text for the button which in this case is the string “True”.

Therefore they are two different things.


#5

[quote=“atexit8”][quote=“Marcos”]
Changing R.id.true_button to R.string.true_button (and the same for the false case) fixes it.

Could you kindly explain why?
Thanks[/quote]

I am not the author, but it seems to me that
R.id.true_button is the button itself
while
R.string.true_button is the text for the button which in this case is the string “True”.

Therefore they are two different things.[/quote]

thank you all…
of course they are two different things…
but why when we use R.id.true_button instead of R.string.true_button it always show false ?
please can any one explain ?


#6

hi there,

I always receive false and I dont know why :confused: any hints?

mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false); Log.d(TAG,"mAnswerIsTrue getIntent(): " + mAnswerIsTrue); CheatActivity.java

this is how I send “true”. I did debug, so for sure I send “true”

Intent i =new Intent(QuizActivity.this, CheatActivity.class); boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion(); Log.d(TAG,"answerIsTrue: " + answerIsTrue); i.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue); startActivity(i); QuizActivity.java

12-16 23:19:55.954: D/QuizActivity(1689): answerIsTrue: true
12-16 23:19:56.370: D/CheatActivity(1689): mAnswerIsTrue getIntent(): false

thx, jave nice day :slight_smile:


#7

i.putExtra([color=#FF0000]CheatAcitivity.[/color]EXTRA_ANSWER_IS_TRUE, answerIsTrue); and it start works :slight_smile: