wood burning stoves 2.0*
The moose likes Android and the fly likes I can't  show random images in Android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "I can Watch "I can New topic
Author

I can't show random images in Android

D Hagy
Greenhorn

Joined: Jul 05, 2011
Posts: 12
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
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
You seem to assume that "(int) Math.random()*3" is the same as "(int) (Math.random()*3)" - have you verified that assumption?


Ping & DNS - my free Android networking tools app
 
Don't get me started about those stupid light bulbs.
 
subject: I can't show random images in Android
 
Similar Threads
it doesn't change the longitude & latitude
Is there any problem with this code?
Android: Add custom widget or view (ex. vertical bar) which changes height programmatically
Android: Cannot show more than one image
getting null pointer