如何在列表视图中存储Array列表的随机值?

时间:2016-03-01 10:28:38

标签: android listview arraylist

我有一个包含多个值的数组列表。我在数组列表上执行随机函数,我想存储每个随机值,例如,只需点击列表视图中的按钮即可。我怎么能这样做?

Int rand;
String[] listv={"one","two","three","four","five"}
rand=(int)(Math.random()*5);
ArrayList<String> planetList = new ArrayList<String>();  planetList.addAll(Arrays.asList(listv[rand]));
adapter = new ArrayAdapter<String>(this, R.layout.txtlay,planetList);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);

1 个答案:

答案 0 :(得分:0)

使用两个不同的列表:第一个包含所有可能的值,第二个包含列表视图的所有随机添加的值。

  1. 创建一个空列表rndValues
  2. 使用该空列表创建适配器
  3. 将适配器设置为listView
  4. 在您的活动中(例如button_click)将listv的随机值添加到rndValues
  5. 在适配器上调用notifyDataSetChanged()以更新列表视图