android - 按字符串查找可绘制对象

标签 android

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/

相关文章:

xml - 在 Android 中构建/使用运行时生成的布局 XML

java - 使用加密货币进行加密。加密失败,解密正常

android - AppWidgetProvider 中的 updateAppWidget 不会导致更新

android - 完成浏览器下载时收到 Intent

android - 为什么 Room 实体不适用于 Android 中的不可变属性

android - DialogFragment 中的工具栏

Android 布局重量不起作用

android - 自定义安卓切换轨道 'animation'

android - TextInputLayout 新增了对字符计数的支持

android - 如何在没有颜色资源的情况下更改 SwipeRefreshLayout 进度背景颜色?