Android get list of string resources from special file

自作多情 提交于 2019-12-25 03:18:45

问题


For example, i have my_string.xml file with strings:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="my_string">My string</string>
    <string name="another_string">Another string</string>
    {...}
</resources>

Is it possible to get this strings to list/hashmap programmatically? Yeh, i know that i can take it by name or something like this. But i need to get it dynamically programmatically, for example, to HashMap<String, String>

Or all string resources will merge together after building and it's not possible to separate it?


回答1:


Put the file in the raw folder. Then you have to open the file

InputStream in = getResources().openRawResource(R.raw.yourfile);

and parse the content manually, for instance save the list as xml or json and parse it and build your desired HashMap or whatever you like. Keep in mind that this may be a blocking operation and should not run on the main UI thread, run it async, but it depends on the length of the file you try to parse, but in general you should run that in an async thread

---- Update You could do something like this:

int stringRes[] = {R.string.my_string, R.string.another_string}
List<String> myStrings = new ArrayList<String>();
for (int id : stringRes){

   String str = getResources().getString(id);
   // TODO do some if check if you want to keep that string or whatever you want to ...
   myStrings.add(str);
}

you could store them into a HashMap(but getResources().getString() acts already like a HashMap ) or List




回答2:


If you want to access all the Strings from the strings.xml file you could use reflection on the R.string class.

Field[] fields = R.strings.class.getFields();
String[] allStringsNames = new String[fields.length];
for (int  i =0; i < fields.length; i++) {           
    allStringsNames[i] = fields[i].getName();
}

You can then store them in Hashmap or wherever you want




回答3:


The whole point of the XML file is to act as a "hashmap" kind of... you can always get a string by using context and the R.string reference. I'm assuming you are aware of this, so then you must be trying to create a simplified reference to this?

Creating a HashMap requires either static references (which are not available for XML) or a runtime creation of the list. You can create the list in the Application class, but I would caution against that. You may have trouble referencing the context but again, you can use the Application constructor to make sure you have a reference to context.

Beyond that, you should check into the StringArray resource. It does not allow you to reference a string using another string because it is an array. Here are the docs:

http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

If it's super-important to use a HashMap then you should probably not use XML and should just create a static class. The XML resource files are primarily used for centralized data (to ease modifications to static strings) and, more so, multi-lingual purposes. Static HashMaps are not the proper use-case.



来源:https://stackoverflow.com/questions/27635641/android-get-list-of-string-resources-from-special-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!