How to enumerate strings in a string resources file?
I have a resources file called string.xml in res/valus directory
the file has many string resources
how can I obtain these items in an array f开发者_如何学编程rom code ?
I tried
Field[] x=R.string.class.getFields();
but it does not work.
thanks
It works fine for me.
You might detail your problem a little more though - what doesn't work?
Note that the R.string class contains ints, not strings. Are you expecting strings? If you want a string from that resourceId int, call Context.getString(resourceId)
.
This is simple example of enumaration strings.You can use it.
Field[] fields = R.string.class.getFields();
for(final Field field : fields) {
String name = field.getName(); //name of string
try{
int id = field.getInt(R.string.class); //id of string
}catch (Exception ex) {
//do smth
}
}
精彩评论