简体   繁体   English

如何从字符串获取资源ID

[英]How to get resource id from String

I have a database column with list of image names. 我有一个带有图像名称列表的数据库列。 I want to put the in imageview using setImageResource. 我想使用setImageResource将其放在imageview中。 In my other application I managed to that like this, but in this application the imageview is not showing anything at all. 在我的其他应用程序中,我设法做到了这一点,但是在此应用程序中,imageview根本不显示任何内容。

String Image1 = db.getImage1Now(RandomIndex);
imageViewDoThis1.setImageResource(getResources().getIdentifier( Image1, "drawable", getPackageName()));

If I do that like this: 如果我这样做:

imageViewDoThis1.setImageResource(R.drawable.image1);

Then it's working.. Help! 然后就可以了。

Use this: 用这个:

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

I think getIdentifier is supposed to take a string as the first parameter. 我认为getIdentifier应该以字符串作为第一个参数。

So I had a problem very similar to this. 所以我有一个与此非常相似的问题。

I had a bunch of images in my resources and I wanted to be able to store which image belonged to each item in the database. 我的资源中有一堆图像,我希望能够存储数据库中属于每个项目的图像。 I ended up using the item's id in the database as a unique identifier for the images. 我最终在数据库中使用了商品的ID作为图像的唯一标识符。 The ids corresponded to constants in my DbIcons class. 这些ID对应于我的DbIcons类中的常量。 When I constructed whatever object I needed, I retrieved the resource Id from that helper class. 当我构造所需的任何对象时,我从该帮助程序类中检索了资源ID。

When I wanted to retrieve the correct image, I would get the id from the database then call the static method getIcon(categoryId). 当我想检索正确的图像时,我将从数据库中获取ID,然后调用静态方法getIcon(categoryId)。 This returned the R.id value, and this was passed to the ImageView. 这将返回R.id值,并将其传递给ImageView。

Here's a snipped of my code. 这是我的代码片段。 Just to make it shorter, I've deleted most of the variables and switch statements: 为了简短起见,我删除了大多数变量和switch语句:

public Category(int id)
    {
        this.id = id;
        this.name = "";
        this.icon = null;
        this.iconResourceId = DbIcons.getIcon(id);
        this.plateIconResourceId = DbIcons.getPlateIcon(id);
    }
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;
        }
    }

I hope this makes sense and helps. 我希望这是有道理的,对您有所帮助。 If you want me to clarify more, just ask. 如果您想让我澄清更多,请问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM