Iteration Through R class
is there any possible way to iterate throught R.raw or R.drawable or any R class? I want to get every id on that folder dyna开发者_如何学运维mically.
ArrayList resArray = new ArrayList();
foreach(int id : R.raw) {
resArray.add(id);
}
or is there any other way?
You can do this using java reflection:
Class raw = R.raw.class;
Field[] fields = raw.getFields();
for (Field field : fields) {
try {
Log.i("REFLECTION",
String.format("%s is %d", field.getName(), field.getInt(null)));
} catch(IllegalAccessException e) {
Log.e("REFLECTION", String.format("%s threw IllegalAccessException.",
field.getName()));
}
}
精彩评论