0

I am using SimpleAdapter for displaying a ListView, but whenever I press the back button, and open the activity (which shows the list) again, the entries of the list become double.

If I do it again, array values again concatenates with the items of the list.

List variable of array is declared as

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

my code in onCreate() of 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} ));
4

1 回答 1

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);
}
于 2012-07-11T21:47:01.093 回答