Loop to display ImageView

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Loop to display ImageView

Postby nah0y » Tue Jan 13, 2009 12:06 pm

Hi there !

I'm a french developper on Android and i have a little problem :)

I want to display for example 4 cards (using ImageView) and i have created events on them to display other images when you're clicking on them, so here is my code :
Code: Select all
        image = (ImageView) findViewById(R.id.image1);
        image.setOnClickListener(new ImageView.OnClickListener() {
           public void onClick(View v) {
              image.setImageResource(R.drawable.face);
              checked(image,R.drawable.face,R.drawable.icon,R.drawable.dos);
           }
        }
        );

        image2 = (ImageView) findViewById(R.id.image2);
        image2.setOnClickListener(new ImageView.OnClickListener() {
           public void onClick(View v) {
              image2.setImageResource(R.drawable.face2);
              checked(image2,R.drawable.face2,R.drawable.icon,R.drawable.dos);
           }
        }
        );
       
        image3 = (ImageView) findViewById(R.id.image3);
        image3.setOnClickListener(new ImageView.OnClickListener() {
           public void onClick(View v) {
              image3.setImageResource(R.drawable.face);
              checked(image3,R.drawable.face,R.drawable.icon,R.drawable.dos);
           }
        }
        );

        image4 = (ImageView) findViewById(R.id.image4);
        image4.setOnClickListener(new ImageView.OnClickListener() {
           public void onClick(View v) {
              image4.setImageResource(R.drawable.face2);
              checked(image4,R.drawable.face2,R.drawable.icon,R.drawable.dos);
           }
        }
        );


What i wanted to know, is how to do that with a loop :)
I mean, something like :

Code: Select all
for(int i = 1; i <= 10; i++) {
image + "i" = (ImageView) findViewById(R.id.image + "i")....
}


Thank you for your answers !
nah0y
Developer
Developer
 
Posts: 30
Joined: Mon Jan 12, 2009 10:59 pm

Top

Postby MrSnowflake » Tue Jan 13, 2009 1:59 pm

You can do this, but then you would have to use reflection, to get the members of R.id, and look out for not trying to find non ImageViews. But this won't give you any advantage over doing it manually, it would make it even worse, because reflection isn't that fast, and checking whether a foundView is an ImageView isn't fast either, so doing this manually is the best way to go.
Or, you could generate the ImageViews dynamically and add them to the Layout of the Activity.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby nah0y » Tue Jan 13, 2009 2:18 pm

Thank you for your answer !!

In fact, i'm trying to create the game pairs : http://www.geocities.com/mutmainaa/quiz ... pairs.html

And i want to automatically generate cards (as ImageView) with levels, for exemple, when i'm level 5, my loop would have create 12 cards and if i'm level 8, 24 cards for example.

Of course, i can create all the level in the same .java file but it would be easier to do that with a loop.
Here comes a question : will the application take a lot of time to run if i have a lot of code for the 10 levels i want to do ?

Thanks again for your answer !
nah0y
Developer
Developer
 
Posts: 30
Joined: Mon Jan 12, 2009 10:59 pm

Postby MrSnowflake » Tue Jan 13, 2009 2:31 pm

Can't you put references to your images in a resource array?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby nah0y » Tue Jan 13, 2009 2:34 pm

Hum... yeah maybe I could (i'm not very familiar with android yet) but why ?
nah0y
Developer
Developer
 
Posts: 30
Joined: Mon Jan 12, 2009 10:59 pm

Postby MrSnowflake » Tue Jan 13, 2009 2:42 pm

I understood you wrong. But as you want to populate the gridView, you can easilly do this in a for loop...
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby nah0y » Tue Jan 13, 2009 2:44 pm

Oh yes, i could do that !

But i havn't used a gridview yet, so i don't really know how to use it... time to practice :)

Thx !
nah0y
Developer
Developer
 
Posts: 30
Joined: Mon Jan 12, 2009 10:59 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests