Android string resource with value


#1

Ok this might seem basic but what i am searching for is, a way to assign some value (integer or long) to string resource.

For eg. I have a string-array in arrays.xml in which each item is a string resource that are translatable. I show this string array directly in a list and let user to choose one of them and save that choice in preferences. But now that saved string might be of any language so we couldn’t really use it, as the language ‘when user changed the preference’ and ‘when preference value is used’ might be different so i need to assign a correspondong value to each of string item in string-array which would remain same even if language changes.
Surely, there could be many ways to do that, is it possible that we add some value to those string items in the xml itself such that we it changes in future we just need to change the value in xml. We also need to show the selected item again when user opens the list.
It might be quite basic but i am looking a way to do it through xml files.


#2

I show this string array directly in a list and let user to choose one of them and save that choice in preferences

Save the index of your array in the preference, not the string.

We also need to show the selected item again when user opens the list.

Load the string array and get the string via the index that you saved in the preference.


#3

Yeah but that way i would still need to add switch cases assigning the value corresponding to each index ( string array has strings but the value actually needed is number e.g. ‘After 3 hours’ -> 3x60x60 ). But it seems there is no simpler way to do it in xml. Thanks for your time :slight_smile:.


#4

i would still need to add switch cases assigning the value corresponding to each index

Or, have an <integer-array> that contains the desired values in the same order, and look up the value based on the index.


#5

Yeah that’s better. Thanks.