【发布时间】:2016-05-30 04:18:09
【问题描述】:
我想用键值对实现android tab。
目前我已在我的代码中使用地图存储它。
我想将这些键值对映射存储在android资源中并从资源中提取。
必须使用存储的密钥进行选项卡更改服务器调用。
这样做的最佳做法是什么。
【问题讨论】:
我想用键值对实现android tab。
目前我已在我的代码中使用地图存储它。
我想将这些键值对映射存储在android资源中并从资源中提取。
必须使用存储的密钥进行选项卡更改服务器调用。
这样做的最佳做法是什么。
【问题讨论】:
如果键值对不是复杂对象,最好的方法是将它们存储在 SharedPreferences 中。 参考:https://stackoverflow.com/a/7944653/1594776
如果它们很复杂,请将其存储在内部存储器中。 参考:https://stackoverflow.com/a/7944773/1594776
如果你还想用 xml 存储,请参考:https://stackoverflow.com/a/10196618/1594776
解析 xml 的函数(来源:https://stackoverflow.com/a/29856441/1594776):
public static Map<String, String> getHashMapResource(Context context, int hashMapResId) {
Map<String, String> map = new HashMap<>();
XmlResourceParser parser = context.getResources().getXml(hashMapResId);
String key = null, value = null;
try {
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
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;
}
【讨论】:
您可以使用 Sharedpreferences 来存储键值对。根据偏好,您可以以键值格式永久存储少量数据。
【讨论】: