How can I save the value selected from a spinner that would change if the device's language change?
So this one is very difficult I guess.
I've been trying all means and all of my knowledge, so after searching all the web and this site I have to ask the question.
Here is the deal:
I have a spinner that has as values (in English) say: bees birds squirrels
In spanish the values will be:
abejas pajaros ardillas
I make sure they are the same because at runtime I construct the values going into the spinner from a file called
store.xml
<resources>
<string name="bees">bees</string>
<string name="birds">birds</string>
<string name="squirrels">squirrels</string>
<string name="other_species">other species</string>
</resources>
and under the folder values-es
store.xml
<resources>
<string name="bees">abejas</string>
<string name="birds">pajaros</string>
<string name="squirrels">ardillas</string>
<string name="other_species">otra esp开发者_StackOverflow社区ecie</string>
</resources>
The problem is that I shouldn't save the actual value selected by the user, because when they change the device's language, then the value saved it would be in a different language.
So I thought in a way of getting the unique Id of the string value selected by the user and I used res.getIdentifier to get the Id and then the name of the string resource. THEN I would save the name of the resource in the database, that way if the language changed I would be able to locate the value based on the name.
Now, res.getIdentifier worked, as long as the value was the same as the name i.e.:
value "bees", name="bees" (works)
value "bees", name="abejas" (don't work)
value "other_species", name="other species" (don't work)
So is there a way that I can get to the name of a string in an xml, from the value?
Or is there another workaround this?
You could fill your spinner with ids rather than strings, and use a custom adapter to display the strings. That way the logic is language independent.
Use another array as a mapping for example:
English Language A
0 2
1 3
2 0
3 1
精彩评论