开发者

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()));
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜