首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SimpleAdapter正在复制项目

SimpleAdapter正在复制项目
EN

Stack Overflow用户
提问于 2012-07-12 05:43:24
回答 1查看 435关注 0票数 0

我使用SimpleAdapter来显示ListView,但是每当我按下back按钮,并再次打开activity (显示列表)时,列表中的条目变为双倍。

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

数组的列表变量声明为

代码语言:javascript
复制
 private static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

我在List.javaonCreate()中的代码

代码语言:javascript
复制
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} ));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-12 05:47:01

您可以使用if语句来检查列表中是否已有内容,如下所示:

代码语言:javascript
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11441951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档