3

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);
            }
        });
}
Since_2008
  • 2,331
  • 8
  • 38
  • 68
  • 1
    Have a look at [How do I iterate through the id properties of R.java class?](http://stackoverflow.com/questions/2941459/how-do-i-iterate-through-the-id-properties-of-r-java-class) and [Programatically iterate through Resource ids](http://stackoverflow.com/questions/3545196/android-programatically-iterate-through-resource-ids). – Matt Ball Nov 02 '11 at 15:55
  • @MattBall - Thanks, the 'How do I iterate through the id properties of R.java class?' had the answer I was looking for. – Since_2008 Nov 03 '11 at 13:55
  • I used the the following resource as MattBall suggested. http://stackoverflow.com/questions/2941459/how-do-i-iterate-through-the-id-properties-of-r-java-class – Since_2008 Nov 04 '11 at 20:17
  • @MattBall - Can you please post as an answer, so I can mark it as correct for now? Thanks. – Since_2008 Nov 07 '11 at 16:46

3 Answers3

1

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
}
Manuel
  • 10,153
  • 5
  • 41
  • 60
1

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.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

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());

}
Will Jamieson
  • 918
  • 1
  • 16
  • 32