Possible Duplicate:
Android - Open resource from @drawable String
首先抱歉标题,但我不知道我可以设置什么标题。
好的,这是我的问题:
我将从外部数据库中提取一个字符串,例如:'picture0001'。
在 res/drawable 文件夹中,我有一张名为 picture0001 的图片。
我想将该图片设置为 ImageView 的背景(源)。
问题是,如何用我从外部数据库得到的字符串查找这张图片。
非常感谢。
最佳答案
是的,您可以使用 Resources.getIdentifier()
按名称查找它.
Context context = imageView.getContext();
int id = context.getResources().getIdentifier("picture0001", "drawable", context.getPackageName());
imageView.setImageResource(id);
效率不高,但可以偶尔查找资源。
关于android - 按字符串查找可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13351003/