Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating hashmap/map from XML resources

Tags:

android

xml

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.

like image 328
KimHafr Avatar asked Jun 10 '10 10:06

KimHafr


2 Answers

Also you can define a map in XML, put it in res/xml and parse to HashMap (suggested in this post). If you want to keep key order parse to LinkedHashMap. Simple implementation follows:

Map resource in res/xml:

<?xml version="1.0" encoding="utf-8"?> <map linked="true">     <entry key="key1">value1</entry>     <entry key="key2">value2</entry>     <entry key="key3">value3</entry> </map> 

Resource parser:

public class ResourceUtils {     public static Map<String,String> getHashMapResource(Context c, int hashMapResId) {         Map<String,String> map = null;         XmlResourceParser parser = c.getResources().getXml(hashMapResId);          String key = null, value = null;          try {             int eventType = parser.getEventType();              while (eventType != XmlPullParser.END_DOCUMENT) {                 if (eventType == XmlPullParser.START_DOCUMENT) {                     Log.d("utils","Start document");                 } else if (eventType == XmlPullParser.START_TAG) {                     if (parser.getName().equals("map")) {                         boolean isLinked = parser.getAttributeBooleanValue(null, "linked", false);                          map = isLinked ? new LinkedHashMap<String, String>() : new HashMap<String, String>();                     } else if (parser.getName().equals("entry")) {                         key = parser.getAttributeValue(null, "key");                          if (null == key) {                             parser.close();                             return null;                         }                     }                 } else if (eventType == XmlPullParser.END_TAG) {                     if (parser.getName().equals("entry")) {                         map.put(key, value);                         key = null;                         value = null;                     }                 } else if (eventType == XmlPullParser.TEXT) {                     if (null != key) {                         value = parser.getText();                     }                 }                 eventType = parser.next();             }         } catch (Exception e) {             e.printStackTrace();             return null;         }          return map;     } } 
like image 90
vokilam Avatar answered Oct 27 '22 23:10

vokilam


Today I came across the same problem and studying developer.android.com 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.

like image 29
Vladimir Avatar answered Oct 27 '22 23:10

Vladimir