Home > Net >  Does Integer can return drawable?
Does Integer can return drawable?

Time:06-23

I'm making an app and I make an Arraylist to store multiple drawables, but for some reason when I try

ArrayList<ImageView> img = new ArrayList<ImageView>();

and I try to store drawables directly from drawable directory

img.add(R.drawable.img1);

It's asking me to change the data type to integer, I don't know how to test this code if it will return the Image or not, so can someone verify what does this code do when I try to get a single value from the ArrayList?

CodePudding user response:

ImageView is UI element that is used to display drawables, it is not the drawable itself.

R.drawables are stored ints indeed and they represent references to those actual drawables you have stored. So if you want to have list of drawables you need to change type of list to int.

And to change which drawable is displayed you can call

.setBackgroundResource(img.get(i)) // where i is index of desired drawable

on UI element in your case ImageView

CodePudding user response:

R.drawable.* returns Int. So you must have to create an ArrayList of integers.

ArrayList<ImageView> img = new ArrayList<ImageView>();
    img.add(R.drawable.img1);
    imageView.setImageResource(img.get(1));

It will work fine.

  • Related