Reputation: 2341
I have 100 images, each is an image of a percentage, for example, 1%, 2%, 3%, etc.
What's the best way to go through each image? Should I add each image resource to a List or Dictionary(if that exists in Android). Or am I forced to hard code it?
public void ShowCircle(final int percentage){
final ImageView ring = (ImageView)findViewById(R.id.ring);
ring.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0; i != percentage; i++)
//I was thinking here I can access list/dictionary records.
ring.setImageResource(R.drawable.percent_1);
}
});
}
Upvotes: 3
Views: 3432
Reputation: 359796
At OP's request, answering with a copypasta from a comment:
Have a look at How do I iterate through the id properties of R.java class? and Android: Programatically iterate through Resource ids.
...though this question should really just be closed as a dup.
Upvotes: 1
Reputation: 926
I would put them into an array like this
public static int[] picArray = new int[100];
picArray[0] = R.raw.img1;
picArray[1] = R.raw.img2;
//etc
and iterate through them like this
public void onClick(View view)
{
ImageView image = (ImageView) findViewById(R.id.imageView1);
if(counter== (picArray.length - 1) )
{
counter =0;
}
else
{
counter++;
}
image.setImageResource(picArray[counter].getImg());
}
Upvotes: 0
Reputation: 10303
You can put them in an array (info on how to add to an array is found here) and then iterate trhough them with an foreach loop like so:
for(ImageView image : ImageArray) {
// Do stuff with your image that is now in the variable called image.
// All images will be called in the order that they are positioned in the array
}
Upvotes: 1