1

Here's my code that's giving me grief.

TextView questionView = (TextView) findViewById(R.id.questionView);
if(questionView.getText().equals(R.string.begginigStatement){
    currentQuestionIndex = -2;
    Log.d(TAG, "the TextView's text is equal to R.string.beggingStatement);
}

I'm trying to compare a string w/ an int but I can't figure out the solution other than perhaps hardcoding the string, though I know that's not a proper convention. What's the solution?

3 Answers 3

5

R.string.begginigStatement is just an ID of the string as generated in R.class. To retrieve the value call:

getResources().getString(R.string.begginigStatement)

1
  • 1
    Also EditText.getText() returns an Editable, not a String. Bryan also needs to use getString(). EditText.getText().toString().equals(getResources().getString(R.string.beginingStatement)) Jul 5, 2015 at 0:16
2

try to use:

context.getResources().getString(R.string.begginigStatement);

and context can be 'getActivity()' if it's in Fragment or just :

getResources().getString(R.string.begginigStatement)

if it has context

0

You have to compare this string values:

questionView.getText().toString().equal(getResources().getString(R.string.begginigStatement))

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.