Hello Guys I am New to Android. I am developing an App but stuck here. I want to set Random Text to TextView and I did it successfully but I am getting same text on all textview I put it. I want if the text set to one textview should not be show to other means its should not be repeat. Any help must be appreciated. I am posting some code here.
int[] array_set_up_text;
Random rand_set_up_text;
rand_set_up_text = new Random();
array_set_up_text = new int[] { R.string.set_up_text1, R.string.set_up_text2, R.string.set_up_text3,
R.string.set_up_text4, R.string.set_up_text5, R.string.set_up_text6, R.string.set_up_text7,
R.string.set_up_text8,R.string.set_up_text9,R.string.set_up_text10,R.string.set_up_text11,R.string.set_up_text12};
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text1.setText(array_set_up_text[rand_text_set_up]);
text2.setText(array_set_up_text[rand_text_set_up]);
text3.setText(array_set_up_text[rand_text_set_up]);
change your code to:
text1.setText(array_set_up_text[rand_set_up_text.nextInt(array_set_up_text.length)]);
text2.setText(array_set_up_text[rand_set_up_text.nextInt(array_set_up_text.length)]);
text3.setText(array_set_up_text[rand_set_up_text.nextInt(array_set_up_text.length)]);
You need to recall the nextInt() method each time:
text1.setText(array_set_up_text[rand_text_set_up]);
rand_set_up_text.nextInt()
text2.setText(array_set_up_text[rand_text_set_up]);
rand_set_up_text.nextInt()
text3.setText(array_set_up_text[rand_text_set_up]);
Your problem is this line
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
You get a random position once, then don't update it.
Easiest solution is to do something like below
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text2.setText(array_set_up_text[rand_text_set_up]);
//Get a new random position.
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text3.setText(array_set_up_text[rand_text_set_up]);
//Get a new random position.
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text3.setText(array_set_up_text[rand_text_set_up]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.