简体   繁体   中英

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. In my other application I managed to that like this, but in this application the imageview is not showing anything at all.

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.

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. The ids corresponded to constants in my DbIcons class. When I constructed whatever object I needed, I retrieved the resource Id from that helper class.

When I wanted to retrieve the correct image, I would get the id from the database then call the static method getIcon(categoryId). This returned the R.id value, and this was passed to the ImageView.

Here's a snipped of my code. Just to make it shorter, I've deleted most of the variables and switch statements:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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