我使用SimpleAdapter来显示ListView,但是每当我按下back按钮,并再次打开activity (显示列表)时,列表中的条目变为双倍。
如果我再次这样做,数组值将再次与列表中的项连接。
数组的列表变量声明为
private static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();我在List.java的onCreate()中的代码
setContentView(R.layout.list);
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("first","Strength");
temp.put("second", strength);
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("first","what");
temp1.put("second", "??");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("first","Time");
temp2.put("second", "time");
list.add(temp2);
HashMap<String,String> temp3 = new HashMap<String,String>();
temp3.put("first","Repeat");
temp3.put("second", "everyday");
list.add(temp3);
setListAdapter(new SimpleAdapter(this,list,R.layout.row_view, new String [] {"first","second"}, new int [] {R.id.rowTextView1, R.id.rowTextView2} ));发布于 2012-07-12 05:47:01
您可以使用if语句来检查列表中是否已有内容,如下所示:
if(list.size() == 0){
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("first","Strength");
temp.put("second", strength);
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("first","what");
temp1.put("second", "??");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("first","Time");
temp2.put("second", "time");
list.add(temp2);
HashMap<String,String> temp3 = new HashMap<String,String>();
temp3.put("first","Repeat");
temp3.put("second", "everyday");
list.add(temp3);
}https://stackoverflow.com/questions/11441951
复制相似问题