1

I've something like this

for (int i=0; i<dbHelper.cantidad_restaurants; i++) {
            datos.add(new clsRestaurants(R.drawable.res_1, "Este es el Restaurant 1", "Esta es la descripcion del REstaurant 1"));
            datos.add(new clsRestaurants(R.drawable.res_2, "Este es el Restaurant 2", "Esta es la descripcion del REstaurant 2"));
            datos.add(new clsRestaurants(R.drawable.res_3, "Este es el Restaurant 3", "Esta es la descripcion del REstaurant 3"));
            datos.add(new clsRestaurants(R.drawable.res_5, "Este es el Restaurant 5", "Esta es la descripcion del REstaurant 5"));
        }

I'd like instead of writing R.Drawable.Res_1 i could write something like

"R.Drawable.Res_ + i "

Where I is the number of the for loop

Rishabh Maurya
  • 1,448
  • 3
  • 22
  • 40
AtarC5
  • 393
  • 2
  • 11
  • 27

1 Answers1

1

you need use getIdentifier and get int id

int resourceId = Activity.getResources().getIdentifier("Res_"+i, "drawable", "your.package.name");
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
  • So then i should pass 'resourceID' instead of R.drawable.? – AtarC5 Jul 18 '17 at 04:56
  • yes, R.drawable. is int number and need pass 'resourceID' instead of R.drawable. – Rasoul Miri Jul 18 '17 at 05:00
  • I'm getting an error... "Error:(27, 42) error: non-static method getResources() cannot be referenced from a static context" My line: "int resourceId = MainActivity.getResources().getIdentifier("Res_"+i, "drawable", "com.soft.kukito.cardviewprueba");" ---- I also tried: "int resourceId = Activity.getResources().getIdentifier("Res_"+i, "drawable", "com.soft.kukito.cardviewprueba");" – AtarC5 Jul 18 '17 at 05:51
  • imgV.setImageResource(resourceId); use this for use resourceId – Rasoul Miri Jul 18 '17 at 05:56
  • But i can't put the the imgV, because i'm developing a dinamcally cardview, so i have only to tell the path where the image is located, changing only the last number. – AtarC5 Jul 18 '17 at 05:59