1. Check out our app, Forums for Android! Download and leave feedback here!

Referencing Resource Via String

Discussion in 'Application Development' started by safibaba, May 5, 2010.

  1. safibaba

    safibaba Member
    Thread Starter
    15

    May 3, 2010
    15
    0
    15
    I was just wondering if there is any way to reference a resource using a string.

    Say you had 10 images in your drawable folder called 'img1' 'img2' 'img3' etc......

    PHP:
    1. int imgNum = 2;
    2. String imgName = "img"+imgNum;
    3. Drawable d = (Drawable) getResources().getDrawable(R.drawable.imgName);
    Is there any way of doing this? Or will I just have to use if statements to work out which image to use?
     

    Advertisement

  2. markb

    markb Well-Known Member
    63

    Sep 14, 2009
    148
    26
    63
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    Hi,

    It might be overkill, but if you want something that will scale up to handle lots of resources then you could use Java reflection.

    I describe a solution in another thread:

    http://androidforums.com/android-de...lements-res-raw-folder-one-shot-run-time.html

    You'd have to adapt the code a little. Instead of:

    Code (Text):
    1. Field fields[] = R.raw.class.getDeclaredFields() ;
    2.  
    You'd have to use:

    Code (Text):
    1. Field fields[] = R.raw.drawable.getDeclaredFields() ;
    2.  
    If you're familiar with Java reflection then hopefully that other thread will give you an idea of how to achieve what you want.

    I can explain further if you like. Just ask.

    Regards,

    Mark
     
  3. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    To answer your first question: no, I don't know a way to reference your drawables by a String.

    To answer your second question: I use Maps to handle problems like yours where I know there are going to be multiple statically paired items.

    For example, I might declare the global variable:
    Code (Text):
    1.  
    2. public static final String PREFIX = "img";
    3. public static final Map mImageMap = new HashMap<String, Drawable>();
    4.  
    ...then, in onCreate I'd pay the one time setup fee by putting:
    Code (Text):
    1.  
    2. if (mImageMap.size() == 0) {
    3.    mImageMap.put(PREFIX + "1", getResources().getDrawable(R.drawable.img1));
    4.    mImageMap.put(PREFIX + "2", getResources().getDrawable(R.drawable.img2));
    5.    mImageMap.put(PREFIX + "3", getResources().getDrawable(R.drawable.img3));
    6. }
    7.  
    and then finally when I want to reference the value, I would do something like:
    Code (Text):
    1.  
    2. int exampleInput = 2;
    3. Drawable d = mImageMap.get(PREFIX + exampleInput);
    4.  
     
    nguyendat likes this.
  4. safibaba

    safibaba Member
    Thread Starter
    15

    May 3, 2010
    15
    0
    15
    Ahh - hashmap - that's a great idea!

    I'm so used to working a a language where this is possible, I didn't even think of making a dictionary / hash.

    Thanks Boogs. I actually just wrote a factory method to return the relevant image, since there were only three, but I will need to do one with much more images soon, so this will be a great solution, and much easier to maintain.

    Cheers:):)
     

Share This Page

Loading...