KimAMartinsen KimAMartinsen - 3 months ago 19
Android Question

Creating hashmap/map from XML resources

I'm making an application where a web service fetches (amongst other) a bunch of codes from a webservice (I.e BEL, FRA, SWE). During runtime I want to translate these codes to their apporiate names to display to users (I.e Belgium, France, Sweden). There can be a lot of these codes, so i'm wondering if there is any approriate way to store the (code, name) entry as a some sort of map in the XML resources in Android, so I can quickly fetch the name by the given code?

It's all about speed here, since the map can have a few hundred entries.


Today I came across the same problem and studying for a long time didn't help since Android resources cannot be hashes (maps), only arrays.

So I found 2 ways:

  1. Have a string array of values like "BEL|Belgium", parse those string early in the program and store in a Map<>

  2. Have 2 string arrays: first with the values of "BEL", "FRA", "SWE" and second with "Belgium", "France", "Sweden".

Second is more sensitive cause you have to synchronize changes and order in both arrays simultaneously.