Search code examples
javaandroidsettext

setText printing out resource id only but not the actual text


I have the following codes. I am trying to get the TextView to display "Question 1: What is your name?" (the question text string stored in the String file), but now it is displaying "Question 1: 2131362201". How do I fix this? Thanks!

AnswerList.java

public class AnswerList {
   private int mQuestion;
       private int mAnswer;

   public AnswerList(int question, int answer){
      mQuestion = question;
      mAnswer = answer;
   }
}

public int getQuestion() {
    return mQuestion;
}

Main.java

private AnswerList[] mQuestionBank = new AnswerList[] {
   new AnswerList(R.string.question1, R.string.answer1)
}

.....

int question = mQuestionBank[0].getQuestion();
mQuestionTextView.setText("Question 1:" + question);

Solution

  • getQuestion() returns an int, so you should expect the id to be shown. In order to show the string resource associated with that id, you need to call getString() with a Context (such as an Activity):

    String question = getString(mQuestionBAnk[0].getQuestion());