在Android中,我正在循环遍历数据库并分配文本和图像:
Cursor res = myDb.getAllData();
while (res.moveToNext()) {
Actors actor = new Actors();
actor.setName(res.getString(1));
String th = res.getString(11);
Integer thumb = this.getResources().getIdentifier(th, "drawable", "mypackage");
actor.setThumb(R.drawable.th);
}
然而Lint建议不要使用getIdentifier - 不推荐使用此函数,因为资源反射使得执行构建优化和代码的编译时验证更加困难。
在数据库列中,我只有图像名称(字符串)。我该如何替换getIdentifier?
即使我将DB列直接更改为,它仍然是一个字符串,但对于setThumb我需要一个可绘制对象。
switch
语句,或者构建和缓存一个字符串到标识符值的Map<String, Integer>
并使用它。 - CommonsWare