Iterate through certain images in res/drawable-mdpi

2.3k views Asked by At

In the res/drawable-mdpi folder I have 26 letter images, named big_0.png to big_25.png.

I would like to add them all (and ignore any other images in the folder) to a hash map:

private static HashMap<Character, Drawable> sHash = 
    new HashMap<Character, Drawable>();

Initially I was planning something like:

private static final CharacterIterator ABC = 
    new StringCharacterIterator("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

int i = 0;
for (char c = ABC.first(); 
    c != CharacterIterator.DONE; 
    c = ABC.next(), i++) {

        String fileName = "R.drawable.big_" + i;
        Drawable image = context.getResources().getDrawable(fileName);
        sHash.put(c, image);
}

But then I've realized that R.drawable.big_0 to R.drawable.big_25 are of type int and not String.

So please advise me, how to iterate through the 26 images correctly?

1

There are 1 answers

1
CommonsWare On BEST ANSWER

Use getResources().getIdentifier() to convert "R.drawable.big_" + i into the resource ID value that you would use with getDrawable().