重新启动活动时无重复的随机数



我有活动A,B和C。

在活动B中,我有随机数生成器和collections.shuffle()。这给了我下一个活动中的随机数c。

然后,我使用按钮返回活动B并再次重新启动整个过程,但是,由于整个collections.shuffle()是重新启动的,因此它在C中具有重复。

当我从B到C时,我需要获取无重复的随机数,反复进行。

活动B:

    public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

Button a = (Button) findViewById(R.id.button1); // Here the R.id.button1 is the button from you design
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
    ArrayList<Integer> randomNumber = new ArrayList<Integer>();
    for (int i = 1; i <= 17; ++i) randomNumber.add(i);
    Collections.shuffle(randomNumber);
    int random = randomNumber.get(0); 
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("Value",random);
startActivity(intent);
}
    });
Button b = (Button) findViewById(R.id.button2); // Here the R.id.button1 is the button from you design
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
    ArrayList<Integer> randomNumber = new ArrayList<Integer>();
    for (int i = 18; i <= 35; ++i) randomNumber.add(i);
    Collections.shuffle(randomNumber);
    int random = randomNumber.get(0); 
    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
    intent.putExtra("Value",random);
    startActivity(intent);
}
    });
Button c = (Button) findViewById(R.id.button3); // Here the R.id.button1 is the button from you design
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
    ArrayList<Integer> randomNumber = new ArrayList<Integer>();
    for (int i = 36; i <= 50; ++i) randomNumber.add(i);
    Collections.shuffle(randomNumber);
    int random = randomNumber.get(0); 
    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
    intent.putExtra("Value",random);
    startActivity(intent);
}
    });

活动C:

    public class Main2Activity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main2);
   Button back = (Button) findViewById(R.id.buttonback); // Here the R.id.button1 is the button from you design
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
odstevalnik.cancel();
Intent i = new Intent(Main2Activity.this, MainActivity.class);
startActivity(i);
}
    });
    Bundle bundle = getIntent().getExtras();
int random = bundle.getInt("Value");
TextView text = (TextView) findViewById(R.id.textView1);
if (random==1) {
    text.setText("blabla");
    image.setImageResource(R.drawable.img2);
    stopnja.setImageResource(R.drawable.stopnja1);
    Toast.makeText(getApplicationContext(), "blabla", 
    Toast.LENGTH_LONG).show();
}
   .....

在活动的OnCreate中创建随机数的集合。以这种方式发送该集合不会重新启动。从列表发送号码

int random = randomNumber.get(i++);

最新更新