Help with messy code?

Put your problem here if it does not fit any of the other categories.

Help with messy code?

Postby ramgraph1 » Fri Jan 25, 2008 12:14 am

I have some pictures that change according to their state. I am using code like this to change them:
Code: Select all
if (pic1 == 1) {
              pic1.setImageResource(R.drawable.blue);
           }
if (pic1 == 2) {
              pic1.setImageResource(R.drawable.red);
           }
if (pic1 == 3) {
              pic1.setImageResource(R.drawable.green);
           }

if (pic2 == 1) {
              pic2.setImageResource(R.drawable.blue);
           }
etc.

Because I have lots of pictures, the code is really long and wastes time whenever I need to change it. The pictures all need unique names but they all change to the same drawables for each state (any that are state 1 are "blue", etc.).
Is there a way to simplify this code and just say, "For all you pictures, if your state is 1 then setImageResource(R.drawable.blue)"?
Thanks for any help you can give!
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Postby Ishtar » Fri Jan 25, 2008 4:16 pm

Hi!

Yes, that would become quite messy :wink:. You should store your pictures, and their states in an array (or if you delete/remove them often, a suitable List). With an array you can easily do this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. int numPictures=5;
  3.  
  4. int state_blue=1;  //declaring this numbers makes your code more readable -> no "magic" numbers
  5.  
  6. int state_red=2;
  7.  
  8. int state_green=3; //these ints could best be final and static, declare them so in your class
  9.  
  10.  
  11.  
  12. Picture []myPictures=new Picture[numPictures];
  13.  
  14. int []states=new int[numPictures];
  15.  
  16.  
  17.  
  18. myPictures[0]=new Picture();
  19.  
  20. states[0]=state_green;
  21.  
  22. //etc
  23.  
  24.  
  25.  
  26. for (int i=0;i<numPictures;i++)
  27.  
  28. {
  29.  
  30.  if (myPictures[i]!=null)
  31.  
  32.  {
  33.  
  34.   switch(states[i])
  35.  
  36.   {
  37.  
  38.   case state_blue:
  39.  
  40.   myPictures[i].setImageResource(R.drawable.blue);
  41.  
  42.   break;
  43.  
  44.   case state_red:
  45.  
  46.   myPictures[i].setImageResource(R.drawable.red);
  47.  
  48.   break;
  49.  
  50.   case state_green:
  51.  
  52.   myPictures[i].setImageResource(R.drawable.green);
  53.  
  54.   break;
  55.  
  56.   default:
  57.  
  58.   // woops! invalid state
  59.  
  60.   }
  61.  
  62.  }
  63.  
  64.  else
  65.  
  66.   //woops! no picture
  67.  
  68. }
  69.  
  70.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


You can't say "for all pictures if your state is.." however, you can loop them all like in this code.
Set the pictures and states and change the "woops" with whatever you want to do there.

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby ramgraph1 » Fri Jan 25, 2008 10:45 pm

Thanks, that's what I was looking for. As someone pretty new to programming I have been rather intimidated by arrays. Hopefully this will help me make friends with them!
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby Ishtar » Fri Jan 25, 2008 10:54 pm

Your welcome!
New to programming and already jumping in an api like android? :o I'll just warn you, itll be a bumpy ride :wink: Don't be afraid to ask your questions.

Happy coding,
Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests