嗨,我有一個字符串數組,像50個字符串,我希望它只選擇10個randon結果並將它們顯示在表格佈局上,我得到的所有東西除了它只顯示一個結果。繼承人我的代碼:從字符串數組中獲取不同的隨機字符串
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));
}
最新怎麼了?
so in labelTV.setText(); 我把q? – SUPLMMM
你需要將'list'中的所有'Strings'連接到一個變量,比如'text',然後調用'labelTV.setText(text)'; –