Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从字符串中获取资源id_Java_Android_Sqlite_Imageview - Fatal编程技术网

Java 如何从字符串中获取资源id

Java 如何从字符串中获取资源id,java,android,sqlite,imageview,Java,Android,Sqlite,Imageview,我有一个包含图像名称列表的数据库列。我想使用setImageResource将图像放在imageview中。在我的另一个应用程序中,我设法做到了这一点,但在这个应用程序中,imageview根本没有显示任何内容 String Image1 = db.getImage1Now(RandomIndex); imageViewDoThis1.setImageResource(getResources().getIdentifier( Image1, "drawable", getPackageName

我有一个包含图像名称列表的数据库列。我想使用setImageResource将图像放在imageview中。在我的另一个应用程序中,我设法做到了这一点,但在这个应用程序中,imageview根本没有显示任何内容

String Image1 = db.getImage1Now(RandomIndex);
imageViewDoThis1.setImageResource(getResources().getIdentifier( Image1, "drawable", getPackageName()));
如果我这样做:

imageViewDoThis1.setImageResource(R.drawable.image1);
然后它就开始工作了。。救命啊

使用以下命令:

imageViewDoThis1.setImageResource(getResources().getIdentifier( "image1", "drawable", getPackageName()));

我认为
getIdentifier
应该将字符串作为第一个参数。

因此我遇到了一个与此非常类似的问题

我的资源中有一堆图像,我希望能够存储属于数据库中每个项目的图像。最后,我在数据库中使用项目的id作为图像的唯一标识符。ID对应于DbIcons类中的常量。当我构造我需要的任何对象时,我从那个helper类中检索了资源Id

当我想要检索正确的图像时,我会从数据库中获取id,然后调用静态方法getIcon(categoryId)。这返回了R.id值,并将其传递给ImageView

这是我的代码片段。为了简化,我删除了大多数变量和switch语句:

public Category(int id) { this.id = id; this.name = ""; this.icon = null; this.iconResourceId = DbIcons.getIcon(id); this.plateIconResourceId = DbIcons.getPlateIcon(id); } 公共类别(int id) { this.id=id; this.name=“”; this.icon=null; this.iconResourceId=DbIcons.getIcon(id); this.plateIconResourceId=DbIcons.getPlateIcon(id); } 公共类DbIcons { /*类别ID*/ 私人最终静态int CAT_BABY=1; 私人最终静态int CAT_烘焙_货物=2; 专用最终静态int CAT_烘焙=3; /*车牌号*/ 专用最终静态int板_BABY=1; 私人最终静态整版烘焙物品=2; 专用最终静态int板_=3; 公共静态int getIcon(int cat) { 开关(cat) { 案例猫宝宝: 返回R.drawable.baby; 箱类烘焙食品: 返回R.drawable.bakedgoods; 箱类烘烤: 返回R.drawable.baking; } 返回R.drawable.default; } 公共静态int getPlateIcon(int plateIcon) { 开关(平板图标) { 婴儿箱板: 返回R.可拉拔婴儿板; 箱板烘焙食品: 返回R.drawable.bakedgoods_板; 箱板烘烤: 返回可拉拔烤盘; } 返回R.drawable.default; } }
我希望这是有意义和有帮助的。如果你想让我进一步澄清,那就问吧

以及
Image1
中的内容。。。如果它是
“image1”
,那么它应该能工作……它是image1,我不明白为什么它不能工作。 public class DbIcons { /* Category Ids */ private final static int CAT_BABY = 1; private final static int CAT_BAKED_GOODS = 2; private final static int CAT_BAKING = 3; /* Plate Ids */ private final static int PLATE_BABY = 1; private final static int PLATE_BAKED_GOODS = 2; private final static int PLATE_BAKING = 3; public static int getIcon(int cat) { switch (cat) { case CAT_BABY: return R.drawable.baby; case CAT_BAKED_GOODS: return R.drawable.bakedgoods; case CAT_BAKING: return R.drawable.baking; } return R.drawable.default; } public static int getPlateIcon(int plateIcon) { switch (plateIcon) { case PLATE_BABY: return R.drawable.baby_plate; case PLATE_BAKED_GOODS: return R.drawable.bakedgoods_plate; case PLATE_BAKING: return R.drawable.baking_plate; } return R.drawable.default; } }