对于getIdentifier
建议,如果资源数量在编译时是固定的,则建议的替代方法是在标识符和资源之间创建静态映射。
因此,例如,你可以使用后缀数字ID:
class Whatever {
static final int[] resources = new int[] {
R.xml.file1, R.xml.file2, R.xml.file3
}
}
这将允许您检索与一个简单的索引操作的资源。
getResources().getXml(resources[i]);
另外,如果你需要一个更具描述性的映射你可以使用Java的Map
基类中的任何一个。
class Whatever {
static final Map<String, Integer> resources = new HashMap<String, Integer>();
static {
resources.put("file1", R.xml.file1);
resources.put("file2", R.xml.file2);
resources.put("file3", R.xml.file3);
resources.put("something_else", R.xml.something_else);
}
}
有了这个,你会然后get(String)
价值其名称。
您可能需要使用反射来获取基于名称的相应ID并传递该ID。 – 2011-04-11 19:04:17