Can't get localized strings from xml files correctly
I've been racking my head with this...
I've got a localized strings.xml file in a values-en folder with this example string: @string/my_string
The string has the following text stored in English: "My String"
When accessing the localized string via a layout, it works fine.
When I try to change it in code, that's where I get problems.
I store the string into an array of strings for later use. The开发者_开发百科 'context' is passed from my activity to a data class and used with this line of code:
dataStrings = new String[] { (String) context.getResources().getString(R.string.my_string) };
Later, I try to display this string, like so:
buttons[0].setText(dataStrings[0]);
It displays:
@string/my_string
How do I get it to display the string without '@string/', the proper localized string?
You can run getString()
directly on the Context
object; you don't need to run getResources()
. However, this should do the same thing as you're currently doing so I don't think that's the source of your problem.
The first thing to confirm is that what you think is happening is happening. Either use the debugger to check that buttons[0]
contains "@string/my_string"
or try calling setText()
with a hard-coded value to make sure the text is actually being updated on the correct button - e.g. buttons[0].setText("StackOverflow!");
精彩评论