Katty
Katty

Reputation: 1747

Add to ArrayList

This is a part of my code:

int n= random.nextInt(images.length - 1);        
iv.setImageResource(images[n]);  

Int n choose one of the indexes randomly and then I used ImageResource to show pictures according to the the random indexes. Now I want to add n to ArrayList. How should I do it? Is there any better way to save a value in memory in android?

Upvotes: 0

Views: 219

Answers (1)

Andro Selva
Andro Selva

Reputation: 54332

Declare a ArrayList like the one below,

ArrayList<Integer> list = new ArrayList<Integer>();

And once you set the Image add that to your ArrayList like this,

int n = random.nextInt(images.length - 1);        
iv.setImageResource(images[n]);  
list.add(n);

Upvotes: 2

Related Questions