I was trying to define a static hash table that makes use of resources, but I got stonewalled by the impossibility of accessing resources statically.
Then I realized that the best of all places to define a static map is in the resources files themselves.
How can I define a map in XML?
I believe that if possible it should be similar to the Listpreference mechanism, with entries and entries-values.
相关问题
- How can I create this custom Bottom Navigation on
- Illegal to have multiple roots (start tag in epilo
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Creating XML Elements without namespace declaratio
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Get Attribute Value From Simple XML Using JQuery /
The correct answer was mentioned by CommonsWare above, but as XML-parsing is not so simple, as following a simple parser for this purpose:
Put this in
res/xml/
, and load it usinggetResources().getXml()
. Walk the events to build up aHashMap<String, String>
.A simpler option would be to use two arrays. This has the benefit of not iterating the xml file again, uses less code and its more straight forward to use arrays of different types.
Then your java code would look like this:
You can always embed json inside your strings.xml file:
res/values/strings.xml
And inside your activity, you can build your map in the
onStart
method:This code needs the google-gson api to work, you can do it using the built in json api in the android sdk.
And As for accessing the Map statically you can create a static method: