skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search