0

Suppose we have :

ImageView imageView_A = findViewById(R.id.imageview_dhze);
ImageView imageView_B = findViewById(R.id.imageview_dhhkjhkze);
ImageView imageView_C = findViewById(R.id.imageview_dhkhkjze);
ImageView imageView_D = findViewById(R.id.imageview_dhhuihuybze);

I want to make a function like this :

changeImages(String NAME) {
    imageView_1_NAME.setImageResource(R.drawable.img);
    imageView_2_NAME.setImageResource(R.drawable.img);
    imageView_3_NAME.setImageResource(R.drawable.img);
}

And for example, I could use it like this :

changeImage("A");

to obtain :

imageView_1_A.setImageResource(R.drawable.img);
imageView_2_A.setImageResource(R.drawable.img);
imageView_3_A.setImageResource(R.drawable.img);

It is possible ? How can I do that ?

Jéwôm'
  • 3,753
  • 5
  • 40
  • 73

4 Answers4

1

You could put all the ImageViews in a HashMap like this:

HashMap<String, ImageView> imageViews = new HashMap<>();

Then you add the ImageViews to the HashMap:

imageViews.put("A", imageView_A);
imageViews.put("B", imageView_B);
...

And your function could look like this then:

private void changeImage(String name) {
    imageViews.get(name).setImageResource(R.drawable.img);
}

In this case you should only call changeImage() when the HashMap contains a key with the same value of String name. If you are not sure if a key exists you need to check if imageViews.containsKey(name) first.

Phil90
  • 138
  • 1
  • 16
0

You can do that:

void changeImage(String NAME) {
    int imageId = this.getResources().getIdentifier("imageView_" + NAME, "id", this.getPackageName());
    if (imageId != 0) {
        ImageView imageView = (ImageView) findViewById(imageId);
        imageView.setImageResource(R.drawable.img);
    }
}
Jéwôm'
  • 3,753
  • 5
  • 40
  • 73
-1

I think what you're looking for is something like this

fun changeImage(which: String) {
    when(which) {
        "a" -> imageView_A.setImageResource(R.drawable.img)
        "b" -> imageView_B.setImageResource(R.drawable.img)
        "c" -> imageView_C.setImageResource(R.drawable.img)
        "d" -> imageView_D.setImageResource(R.drawable.img)

        else -> throw IllegalArgumentException("Unknown image view")
    }
}
Jefferson Tavares
  • 983
  • 1
  • 8
  • 23
-1
int[] images = {R.id.imageA,R.id.imageB,R.id.imageC,R.id.imageD,R.id.imageE};
int[] drawables = {R.drawable.imageA,R.drawable.imageB,R.drawable.imageC,R.drawable.imageD,R.drawable.imageE};
for(int i=0;i<5;i++)
{
    findViewById(images[i]).setImageResource(drawables[i]);
}

It's easier.

Curio
  • 1,331
  • 2
  • 14
  • 34
  • @Shark why is it? – Curio Jun 08 '17 at 16:54
  • because it's not referencing `declared fields` but rather their `R.id` identifiers, and relies on a hardcoded array of them. not dynamic at all, doesn't fit the question of `can I do setImage("Z") to change ImageView_Z member expecting it to do ImageView_Z.setImageResource()` One has to go thru the layout file and add all those identifiers to the array. – Shark Jun 08 '17 at 16:59