2014-01-22 82 views
0

我有活動A,B和C.重新啓動活動時沒有重複的隨機數

在活動B中,我有隨機數生成器和Collections.shuffle()。 這給了我下一個活動C中的隨機數。

然後我使用按鈕返回到活動B並重新啓動整個過程,但它給了我C中的重複,因爲整個Collections.shuffle()重新啓動I猜測。

當我從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(); 
} 

    ..... 
+0

你能舉例說明你想要的嗎?當從B調用活動C並獲得例如數字'6'時,您希望將來的隨機數據中有'6',請更正? – NitroNbg

+0

準確地說,當從B繼續C時,我想要6出來。 – user3041073

回答

1

創建隨機數的集合中的活動B的OnCreate發送這樣的集合將無法重新啓動。從列表中發送號碼

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