I m trying to make a random number generator in android. My code has to start generating numbers in sets of 3 after clicking the “generate” button. So far i’ve coded a generator that can produce finite sets of 3 numbers each. What i want to create is a dynamic generator that keeps generating numbers.CODE:
`
public class Plot2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Random myRandom = new Random();
Button buttonGenerate = (Button)findViewById(R.id.generate);
final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);
buttonGenerate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<Object> Arry1 = new ArrayList<Object>();
for(int i=0;i<5;i++){
ArrayList<Integer> Arry = new ArrayList<Integer>();
for(int k=0;k<3;k++){
Arry.add(myRandom.nextInt(100));
}
Arry1.add(Arry);
}
textGenerateNumber.setText(String.valueOf(Arry1));
}
});
}
}
Do the same process in a loop and update an array from a background thread. For background thread look below link this may help you.
However if you don’t want to generate frequently, Put a sleep() in your background thread for a second and than let it update your Array.
http://developer.android.com/reference/android/os/AsyncTask.html
Happy coding,
Krio
Article which may help,
http://www.dailygeek.de/using-asynctask-to-update-a-listactivity/
Above article shows how you can update your ListActivity in similar fashion you may update your Array.
Lets say create an activity which has AsycTask, and create a separate class such as,
Above class will have an Array list of numbers being generated,
And update this class’s object from Background which eventually available to your UI Thread too. I hope I and clear enough.