2012-07-11 97 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