This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I can't show random images in Android

 
D Hagy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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


 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic