Skip to content Skip to sidebar Skip to footer

Trying To Use Arraylist To Hold Image Resources

In my app, I have a bunch of images in my drawable folder which I select at random and display using imageView. I've been told about ArrayList's which can add/remove objects from

Solution 1:

Use List<Integer> imageHolder = new ArrayList<Integer>();

Solution 2:

ArrayList contains Objects, always, never primitive types. When you set ints into it, they are autoboxed to Integer objects, when you get them back, you get Integer objects as well. A short fix will be:

image.setImageResource((int)imageHolder.get(randInt));

Be careful though, unboxing a null pointer will cause a NullPointerException, So make sure your randInt is in the range of the arraylist.

EDIT:

I totally missed that, but You initialize your ArrayList like that:

ArrayListimageHolder=newArrayList(); 

Which creates ArrayList of Objects. instead, initialize the ArrayList like the following to create ArrayList of integers:

List<Integer> imageHolder = new ArrayList<Integer>(); 

Post a Comment for "Trying To Use Arraylist To Hold Image Resources"