1

just started dabbling in Android and have been told that Strings shouldnt be hard coded into Java but put into the string resource folder.

I have existing code where in a hashmap the key and value are both strings.How would i go about putting the strings into xml and taking them out of the java part completely?

Should I just create a string in the string resource folder like so

<string name="hello">Hello.world</string>

with hello being the key and world being the value and then just parse the string using the . as a delimiter?

Hopefuly I've explained myself correctly

SocialistPig
  • 109
  • 8
  • 1
    Yes that's correct.Check these links if you have more queries http://stackoverflow.com/questions/2437891/java-hashmaps-and-using-strings-as-the-keys-does-the-string-value-get-stored?rq=1 or http://stackoverflow.com/questions/3613722/can-one-combine-android-resource-strings-into-new-strings?rq=1 – Jason Wood Oct 23 '12 at 14:20

4 Answers4

3

There are many other resource types that you can define in the res folder. You can see the available ones here and here. For your purpose, One approach could be defining an array in res/values/arrays.xml:

<resources> 
    <string-array name="hashmap"> 
        <item>key1</item> 
        <item>value1</item> 
        <item>key2</item> 
        <item>value2</item> 
        <item>key3</item> 
        <item>value3</item> 
    </string-array> 
</resources> 

and in the code:

Resources res = this.getResources();
String[] hashmapData = res.getStringArray(R.array.hashmap);
HashMap<String, String> map = new HashMap<String, String>(); 
for(int i=0; i<hashmapData.length; i=i+2) { 
    map.put(hashmapData[i], hashmapData[i+1]);         
} 
Arda Yigithan Orhan
  • 1,062
  • 11
  • 18
1

It really depends on what you're using it for. The 'only include strings in the XML' dogma is great, but it really applies to strings you're using for user output, so that they can easily be changed if, say, you wanted to translate the app. If you're using the key/value-pair for internal workings, there's no need to.

If it does concern output strings, then yes, this would be a good way to go about it. You could also consider splitting the key and value over two XML nodes - it really depends on what you're doing with them.

But in general, yes, this is the way.

Joost
  • 4,094
  • 3
  • 27
  • 58
0

Correct. You should not be hard coding strings into your app. the reason for this is so that you can support localization (multiple languages) in your app very easily. Your strings should all go in /res/values/strings.xml and then if you wanted to support Spanish those strings (with the same name= value) would go in /res/values-es/strings.xml. Then to access these strings in xml you just use @string/hello and in java use getString(R.string.hello). You can find more information at these links:

James McCracken
  • 15,488
  • 5
  • 54
  • 62
0

You have three options really as there is no official support for key/value pairs

  1. There is the approach you've already used, but firstly I'd recommend using a string array instead of a string. This way you can keep the hashmap in one place. I would suggest using a random unicode value instead of a full stop because this will reduce risk when splitting the string.

  2. Have 2 string arrays. First array contains they keys, and the second array contains the value. This approach is generally used, but you have to worry about coupling/making sure any edits reflect on both string arrays.

  3. This one builds on number 2. You have a string array of key/value pairs, and then an array which contains the arrays. You fetch "hashmap" with obtainTypedArray(), then in a for loop or something, use TypedArray.getTextArray() to return the individual String array. This allows for coupling of keys and values.

    <string-array name="hello_world"> <item>Hello</item> <item>World</item> </string-array>

    <array name="hashmap"> <item>@array/hello_world</item> </array

(Please ignore the terrible formatting, stackoverflow doesn't seem to take well to xml)

jimmithy
  • 6,360
  • 2
  • 31
  • 29
  • Thanks all for the replys.Very new to XML and didnt realise they can use arrays(somethingelse to look into!).While I dont think I'll be putting it in to anyother language other than english I'll think I will put it into XML if anything just for the practice – SocialistPig Oct 24 '12 at 09:21