Android Load image on ImageView using arraylist

android, java

Solution

You are defining an ArrayList for Strings. However you need integers as resource identifiers.

Do it instead like this;

ArrayList<Integer> array_image = new ArrayList<Integer>();
array_image.add(R.drawable.image6);
array_image.add(R.drawable.image1);

Problem

This might be a problem that I might overlook again but if you could help me solve it, I will be very thankful. Using this code to get images from drawable and sending it to Imageview.setImageResource (in which my Imageview is inside a gridview); everything works well ``` private Integer[] imageIDs = { //R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5, R.drawable.image6, R.drawable.Image7, }; //Here is my another code to call imageIDs as ImageResource ImageView iv = (ImageView)v.findViewById(R.id.grid_item_picture); iv.setImageResource(imageIDs[position]); //position is a default value of gridview starting from 0 ``` Now what I trying to accomplish is to delete that array and convert it to ArrayList simply because need these inside data to be dynamic. And so I've created this new code ``` ArrayList<String> array_image = new ArrayList<String>(); array_image.add("R.drawable.image6"); array_image.add("R.drawable.image1"); ``` and set the ImageView ImageResource to this code ``` iv.setImageResource(array_image .get(position)); ``` I am getting an error saying: The method setImageResource(int) in the type ImageView is not applicable for the arguments (Object) Can you help me fix/figure it out?

Original source