Android : How to generate random order for a strings list?

on Tuesday, October 14, 2014


Every time an user click on the button it has to show "John - Sue" or "Sue - John".


I tried with this code:



public class MyActivity extends Activity {
ArrayList<String> names = new ArrayList<String>();
int p1, p2;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);

names.add("John");
names.add("Sue");

Button b = (Button) findViewById(R.id.mybutton);
b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

p1 = (int)Math.random();
if (p1 == 0)
p2 = 1;
else
p2 = 0;

String msg = names.get(p1) + " - " + names.get(p2);

AlertDialog msgbox = new AlertDialog.Builder(About.this).setTitle("Click here").setMessage(msg).create();
//msgbox.setPositiveButton("OK", null);
msgbox.setCancelable(true);
msgbox.show();

TextView textView = (TextView) msgbox.findViewById(android.R.id.message);
textView.setTextSize(16);
}
});
}
}


But i get always the same order, even i close and run again the app. How to do that?


0 comments:

Post a Comment