SimpleAdapter复制项目

时间:2012-07-11 21:43:24

标签: android listactivity simpleadapter

我正在使用SimpleAdapter来显示ListView,但每当我按下后退按钮,然后再次打开活动(显示列表)时,列表的条目就变为双倍。

如果我再次这样做,数组值会再次与列表中的项目连接。

数组的列表变量声明为

 private static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

onCreate() List.java中的代码:

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} ));

1 个答案:

答案 0 :(得分:1)

您可以创建一个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);
}