• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

I can't show random images in Android

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice day!
I tried to show random images in my application in an ImageView. I
tried this code:
public class rr extends Activity{
        ImageView secret;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.rr);
                TextView txt = (TextView) findViewById(R.id.txt);
                secret = (ImageView) findViewById(R.id.secret);
                Runnable r1 = new Runnable() {
                        @Override
                        public void run() {
                                // TODO Auto-generated method stub
                                int imageArr[] = new int[3];
                                imageArr[0] = R.drawable.r1_mirror;
                                imageArr[1] = R.drawable.s1_mirror;
                                imageArr[2] = R.drawable.p2_mirror;
                                int n = (int) Math.random()*3;
                                secret.setImageResource(imageArr[n]);
                        }
        };
                secret.postDelayed(r1, 5000);
        }
}

But it only show the r1_mirror image, everytime, when I start my
application in the emulator. What I made bad? I thought that it will
working, but.
Thanks for any helps


 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to assume that "(int) Math.random()*3" is the same as "(int) (Math.random()*3)" - have you verified that assumption?
reply
    Bookmark Topic Watch Topic
  • New Topic