Get all string resources from XML resource file?

左心房为你撑大大i 提交于 2019-12-04 16:48:18

I wonder why would you need to do this. Still, you can use the generated R class to iterate over all kinds of resources.

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

You can declare your strings like this.

 <string-array name="fruitcategory_array">

    <item>Apple</item>
    <item>Bananas</item>
    <item>Mangoes</item>
    <item>Grapes</item>
    <item>Other</item>

 </string-array>

In your activity class, you can access them like the following.

String[] categories;

categories=getResources().getStringArray(R.array.fruitcategory_array);

Just store your Local Strings in an Array in your suggestions.xml file.

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