0

I have 35 images named from pic1.png to pic35.png in my res\drawable folder. I need to create array of this pics to fill my ListView. I know I need some loop.

int[] img;
for (int i = 1; i <= 35; i++) {
            //here should be my loop body, but i dunno :( i tried this, but ofc this is wrong.
            img = R.drawable.pic[i];
        }

One more question: is there any chance to manage my images in drawable folder? I mean, if I have two packs of images, I can't do res\drawable\pack1 and res\drawable\pack2 folders, is the only way to manage images to give em names like pack1_pic1 etc?

eleven
  • 6,779
  • 2
  • 32
  • 52
  • You can get drawable by string name. See here: http://stackoverflow.com/questions/4427608/android-getting-resource-id-from-string – eleven Jan 27 '17 at 11:00

2 Answers2

1

Try this

for (int i = 1; i <= 35; i++) {
   Drawable drawable = getResources().getDrawable(getResources()
                  .getIdentifier("pic"+i, "drawable", getPackageName()));
}
Karthik CP
  • 1,150
  • 13
  • 24
1

Answer to your first question:

you can set image id to your int array using:

int[] img;
for (int i = 1; i <= 35; i++) {
            img[i] = getResources().getIdentifier("pic"+i, "drawable", getPackageName());
        }

which would return the value of R.drawable.pic1...35;

Regarding your second question

No, the resources mechanism doesn't support subfolders in the drawable directory,

see this: Answer

Community
  • 1
  • 1
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62