Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R.string.XXX returns an int from strings.xml when I need a String [duplicate]

This is sort of a continuation of this thread:

Localizing strings in strings.xml gives NullPointerException

This issue arose after going through and implementing the solution provided in that thread. The solution provided worked for instances like this:

loading = (TextView)findViewById(R.id.loading);
loading.setText(R.string.loading);

This does not work for situations like below:

exodus.add(new Question(R.string.a, R.string.b, R.string.c, R.string.d, 0, R.string.e, -1, R.string.f));

The error is The constructor Question(int, int, int, int, int, int, int, int) is undefined. The contructor for Question is supposed to be Question(String, String, String, String, int, String, int, String) but these:

R.string.X

are returning as ints instead of Strings.

What can I do to fix this problem? And also, if you know, why is this method working for setting text of TextViews but not parameters in objects?

like image 860
Matt Avatar asked May 03 '13 17:05

Matt


People also ask

How do I get string resources in activity?

android:text="@string/hello" /> String string = getString (R. string. hello); You can use either getString(int) or getText(int) to retrieve a string.

Which of the format string is not valid?

Format string XXX is not a valid format string so it should not be passed to String.

In which folder can you find the string resource file strings xml?

You can find strings. xml file inside res folder under values as shown below.

Should Use @string resource in Android Studio?

You should add them to a string resource file and then reference them from your layout. This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings. xml file. It is also extremely useful for supporting multiple languages as a separate strings.


2 Answers

R.string.* is a reference to an int in R.java that points to your actual String.

Use context.getResources().getString(R.string.*); to get the actual String value.

like image 101
Raghav Sood Avatar answered Sep 21 '22 00:09

Raghav Sood


Please refer to this StackOverFlow Question

String mystring = getResources().getString(R.string.mystring);
like image 38
MDMalik Avatar answered Sep 21 '22 00:09

MDMalik