• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 43081
77
  • 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?
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic