2012-07-11 99 views
0

我使用SimpleAdapter來顯示ListView,但每次按下後退按鈕並再次打開活動(顯示列表)時,列表中的條目將變爲雙倍。SimpleAdapter複製項目

如果我再次這樣做,數組值再次連接列表中的項目。陣列的

表變量聲明爲

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

我的代碼中的List.javaonCreate()

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

什麼是你沒有聲明的對象「list」它完全在這個代碼中,請發佈你的完整代碼。 – FoamyGuy 2012-07-11 21:45:47

+1

將問題標記爲'* urgent *'不會幫助!你有什麼嘗試? – t0mm13b 2012-07-11 21:48:33

+0

在所有這些代碼之前嘗試list.clear()? – ByteMe 2012-07-11 21:53:47

回答

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

感謝alotttttttttttttttttttttttttttttttttt老兄,你可能活得長久 – Haris 2012-07-11 21:59:38