Referencing R.drawable.my_resource

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

Referencing R.drawable.my_resource

Postby niko001 » Sat Jul 11, 2009 11:52 pm

Hi,

this is more of a basic Java question, but I hope you can help me, too :)!

I would like to loop through a set of images that I've made available in the res/drawable of my app.

The images are named
image_1.jpg
image_2.jpg
image_3.jpg
...


What I would now like to do:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. for (int i = 0; i < 50; i++) {
  3.  
  4.                image_container.setBackgroundResource("R.drawable.image_" + i);
  5.  
  6.             }
  7.  
  8.  
  9.  
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

which doesn't work for obvious reasons. But how do I "dynamically reference" the drawable-resource correctly? What do I need to put instead of ("R.drawable.image_" + i)?

The code in general works, i.e. when I call
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. image_container.setBackgroundResource(R.drawable.image_1);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
for example. It's just the dynamic part that I can't get to work.

Thank you for your help,
Niko
niko001
Freshman
Freshman
 
Posts: 2
Joined: Sat Jul 11, 2009 11:42 pm

Top

Postby niko001 » Sun Jul 12, 2009 5:56 pm

Never mind, I solved it by iterating through an array (=explicitly listing each resource item).
niko001
Freshman
Freshman
 
Posts: 2
Joined: Sat Jul 11, 2009 11:42 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests