I've just begun learning to create Android apps, and I'm struggling with what seems like a very elementary problem.
I'm trying to create a HashMap which is loaded from an XML. However, I'm unable to figure out what the resource ID for a Map stored this way would be. I've used the solution to this problem: Creating hashmap/map from XML resources .
The issue I'm facing is how exactly to structure the res folder and how to reference my data in that res folder.
Here's what I currently have:
Resource Parser 1 :
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;
...
}
return map;
}
}
My res/xml/map.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<map name = "testMap" linked="true">
<entry key="key1">value1</entry>
<entry key="key2">value2</entry>
<entry key="key3">value3</entry>
</map>
So to get hold of the Map, I need to correctly enter the resource ID into my call to getHashMapResource, but I can't find how to do that! I've tried:
R.string.map
R.map.testMap
R.xml.map
R.xml.testMap
I'm out of ideas. I'm sure the problem is trivial to someone who knows this, but I couldn't find anything helpful around the place. Thanks :)
package com.plumtestongo.sample;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.xmlpull.v1.XmlPullParser;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Map<String,String> map = getHashMapResource(this,R.xml.map);
Log.i("Hello",map.toString());
}
public 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;
}
}
I tested this code it works. All you need is use R.xml.map .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.