[android-developers] discovering string resources at run-time
hi, i am making an app that has a bunch of questions multiple choice
answers. i have these in the strings.xml resources file.
my app does not know how many questions or answers there is, so it
wants to discover this at runtime. i.e. i want to look for strings
or string arrays that look like "question1" or "question1Answer2".
i have tried to introspect the stuff in R using the code below. this
code finds the stuff in R just fine.
i was warned away from this approach (see
https://groups.google.com/forum/#!topic/android-developers/rs_l8I4nNtg).
i would like to avoid parsing the xml.
and this seems to work.
is this a reasonable ting to do?
or would i be better off, just keeping my data in resource bundles or
properties file.
thanks
static Map<String,Object> fields(Class<?> c){
Map<String,Object> map=new LinkedHashMap<String,Object>();
Field[] fields;
fields = c.getDeclaredFields();
System.out.println("getting fields for: "+c.getName());
for (Field field : fields)
try {
Object object=field.get(null);
System.out.println("**** "+field.getName()+"="+object);
map.put(field.getName(),field.get(null));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
System.out.println(map);
return map;
}
static void fields() {
Map<String, Object> layout = Jata0Activity.fields(R.layout.class);
System.out.println("layout: " + layout);
Map<String, Object> id = Jata0Activity.fields(R.id.class);
System.out.println("id: " + id);
Map<String, Object> string = Jata0Activity.fields(R.string.class);
System.out.println("string: " + string);
Map<String, Object> array = Jata0Activity.fields(R.array.class);
System.out.println("array: " + array);
}
---
co-chair http://ocjug.org/
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home