Shannon Cole
Shannon Cole

Reputation: 362

How do a load a specific drawable by using a string/int?

If I have a variable with the number associated with a gridview position (id int 14) how do I make the code below load the correct drawable? (ie hccat14)

    mBitmap = getImageFromResource(getContext(), R.drawable.hccat14,w, h);  

Thanks, Shannon

Upvotes: 0

Views: 253

Answers (2)

Bill Gary
Bill Gary

Reputation: 3005

Here's what I use, just set the stringName on click

imageResource = Classname.this.getResources().getIdentifier(stringName,
                null, null);
        mBitmap.setImageResource(imageResource);

EDIT I store the image name as packagename:drawable/imagename in my database

Upvotes: 1

Brian ONeil
Brian ONeil

Reputation: 4339

I don't know exactly how you are trying to use it, but it should be something like this.

   String resouceName = "hccat" + Integer.toString(14);
    int resourceID = getResources().getIdentifier(resourceName,
        "drawable", getPackageName());

Upvotes: 2

Related Questions