Java – How does Android TextView.SetText(int resid) work?

How does Android TextView.SetText(int resid) work?… here is a solution to the problem.

How does Android TextView.SetText(int resid) work?

This is an example from the Big Nerd Ranch Guide:

package com.example.geoquiz;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends ActionBarActivity {
    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;
    private TrueFalse[] mQuestionBank = new TrueFalse[] {
            new TrueFalse(R.string.question_africa, true),
            new TrueFalse(R.string.question_americas, false),
            new TrueFalse(R.string.question_asia, false),
            new TrueFalse(R.string.question_mideast, true),
            new TrueFalse(R.string.question_oceans, true)
    };
    private int mCurrentIndex = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        mTrueButton = (Button)findViewById(R.id.true_button);
        mFalseButton = (Button)findViewById(R.id.false_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 TODO Auto-generated method stub
                Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
            }
        });
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 TODO Auto-generated method stub
                Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
            }
        });

mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
        int question = mQuestionBank[mCurrentIndex].getQuestion();
        mQuestionTextView.setText(question);
    }

@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;
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
         Handle action bar item clicks here. The action bar will
         automatically handle clicks on the Home/Up button, so long
         as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

The problematic part is this:

    int question = mQuestionBank[mCurrentIndex].getQuestion();
    mQuestionTextView.setText(question);

question is an int here, and I’m wondering how it works. What’s even stranger is that if I change question to a literal int, e.g. 1, then the app won’t run.

Solution

The Docs I think this describes better, So I’m not sure if they’ve changed them recently (or since I last looked at them).

Anyway, when you provide int as param, it refers to the resource id. So, if you have a string resource in your strings.xml, you can provide it here instead of the literal string.

So if you have it in your strings.xml

<resources>
    <string name="hello">Hello!</string>
</resources>

You can do it

myTV.setText(R.string.hello);

and myTV will display “Hello”.

You get an exception when you pass an int to it and it doesn’t match id in R.strings.

Related Problems and Solutions