tags:

views:

47

answers:

1

What i m trying to do is, just showing numbers from 1 to 100 in a textview, randomly, and keep on changing until the view is active.

But i am not able to continuously change random number when view is active.

it is just like a timer showing decrement from 10 to 0, but the only difference is i need to show random numbers in between 0 to 100.

How can show this on my view this is how i am trying to do something..may be a better approch will help me out

if (hasFocus) {
   try {  
      //Give the currentTimeMillis some time for the seed  
      Thread.sleep(2);  
   } catch (InterruptedException ex) {  
      ex.printStackTrace();  
   }  
   Random randomizer = new Random(System.currentTimeMillis());  
   // abcdef  = randomizer.nextInt(max-min+1)+min;
   return randomizer.nextInt(max-min+1)+min;  
} else { }
A: 

What do you mean with until the view is active? I am not sure how you are realizing this in your code, but could it be the case that you are actually writing the numbers into your view in a loop? In that case you are probably updating the UI to often which will lead to a crash or unwanted behavior. I'd suggest to put a delay between two different numbers. Maybe I misunderstood your question though. Some code showing what you are trying to do might be helpful.

Pandoro
i edited my question, u can look at ma code.I just wanted to randomize number and keep showing, with refershing or something like dat, my view should show random no cotiniously.regards
shishir.bobby
Well if you just want to write random numbers into a TextView, I would create a Thread with an endless loop. Just use (int)(Math.Random()*100) to get the number and store it in some global variable. Then use a Handler to tell the Activity there is a new number and let the Handler update the TextView. After you did that just sleep the thread for like 200ms. You will need a loop and loops in your main activity are something bad to do. A Thread will be needed. You can check this tutorial on how to use a handler: http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
Pandoro
u took me wrong.i dont want that,i just need to show some random numbers in b/w 1 to 100, on a textview, which should keep on changing automatically.thats all..m i clear dude?
shishir.bobby
Well unless I still don't get you, that is exactly what I told you. Here is the code to do it. http://pastebin.com/cc2PLA10 Just paste it in a project and get an main.xml with a TextView in it under the id TextView01. If this doesn't explain it then well I can't help you.
Pandoro