嗨,我有一个包含 50 个字符串的字符串数组,我希望它只选择 10 个随机结果并将它们显示在表格布局上,除了它只显示一个结果之外,我得到了所有正确的结果。这是我的代码:
private String[] list;
private static final Random rgenerator = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.layout1);
Resources res = getResources();
list = res.getStringArray(R.array.names);
String q = list[rgenerator.nextInt(list.length)];
int total = 10;
for (int current = 0; current < total; current++) {
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100 + current);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
TextView labelTV = new TextView(this);
labelTV.setId(200 + current);
labelTV.setText(q);
labelTV.setTextSize(14);
labelTV.setGravity(Gravity.CENTER);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
tablelayout.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
怎么了?