ListView怎么清空以前的内容啊?

3D剁手网 2011-05-12 01:32:20
public void updateAdapter()
{

setListAdapter(new SimpleAdapter(this, getData(),
R.layout.paperlist, new String[] { "paper_name", "paper_id" },
new int[] { R.id.paper_name,R.id.paper_id}));
}


public List<HashMap<String , String>> getData()
{
System.out.println("list的长度"+list.size());
for (int i = 0; i < 2;i++)
{
HashMap<String, String> map1 = new HashMap<String, String>();
map1.clear();
map1.put("paper_name", MyContentHandler.hisname[i]);
map1.put("paper_id", MyContentHandler.id[i]);
list.add(map1);
}

System.out.println("循环结束后list的长度"+list.size());

return list;
}

我写了第一个函数 updateAdapter(),里面是显示一个ListView,ListView从getData()这个函数获取数据,
但是我发现当我多次调用updateAdapter()时,发现ListView并不是只显示2个子项,而是不断的在后面增加,成
2,4,6,8。。,递增,请问怎样才能做的不是从末尾增加而是清空后在从新写入呢?
...全文
9102 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
DDeadfish 2012-07-18
  • 打赏
  • 举报
回复
用list.clear()就行了..
changy 2011-05-16
  • 打赏
  • 举报
回复
再调用一次setAdapter,设置一个空数据集的Adapter即可
MilanShva 2011-05-13
  • 打赏
  • 举报
回复
写错了,是
list的长度0;
循环结束后list长度2;

list的长度2;
循环结束后list长度6;
所以我觉得是list.add(map1)这里的问题,add的位置不对。
MilanShva 2011-05-13
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ameyume 的回复:]
引用 6 楼 k745933923 的回复:

引用 4 楼 ameyume 的回复:
调用removeAllViewsInLayout(),清空以前的数据

可是为什么我这里没有list.removeAllViewsInLayout();这个方法啊?
我是这么定义的
static ArrayList<HashMap<String, String>> list=new ArrayLi……
[/Quote]
兄台,现在的情况是我代码中有两个System.out.println()函数,
他们每次输出的情况是
list的长度0;
循环结束后list长度2;

list的长度0;
循环结束后list长度6;
直接就从2跳到6了。
而且应为我参照的ListView代码中,并没有显示的定义ListView对象,他是直接在布局文件中声明后就能用了。
ameyume 2011-05-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 k745933923 的回复:]

引用 4 楼 ameyume 的回复:
调用removeAllViewsInLayout(),清空以前的数据

可是为什么我这里没有list.removeAllViewsInLayout();这个方法啊?
我是这么定义的
static ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String, String……
[/Quote]
list是指ListView对象,不是List,不好意思,没说清。
dreamList 2011-05-13
  • 打赏
  • 举报
回复
应该是清adapter

然后重新设置adapter
fujian0910 2011-05-13
  • 打赏
  • 举报
回复
你是写了个子类继承BaseAdapter不 你可以在getview中调用adapter的notifyDataSetChanged()方法进行item内容的刷新
jayqean 2011-05-13
  • 打赏
  • 举报
回复
list.clear()
在调用listview的notify...Change() 刷新下
傲慢的上校 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 k745933923 的回复:]

引用 4 楼 ameyume 的回复:
调用removeAllViewsInLayout(),清空以前的数据

可是为什么我这里没有list.removeAllViewsInLayout();这个方法啊?
我是这么定义的
static ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String, String……
[/Quote]去掉static 试一下
MilanShva 2011-05-12
  • 打赏
  • 举报
回复
listView最烦了,帮顶!
3D剁手网 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 qq1052121189 的回复:]
试一下

引用 1 楼 drsmart 的回复:

清空 你的adapter即可,
[/Quote]
可否以代码详示?
3D剁手网 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 drsmart 的回复:]
清空 你的adapter即可,
[/Quote]
可否以代码详示?
3D剁手网 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ameyume 的回复:]
调用removeAllViewsInLayout(),清空以前的数据
[/Quote]
可是为什么我这里没有list.removeAllViewsInLayout();这个方法啊?
我是这么定义的
static ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String, String>>();
ameyume 2011-05-12
  • 打赏
  • 举报
回复
list.removeAllViewsInLayout();
ameyume 2011-05-12
  • 打赏
  • 举报
回复
调用removeAllViewsInLayout(),清空以前的数据
gujiaogu 2011-05-12
  • 打赏
  • 举报
回复
SimpleAdapter自己不可以清空数据
然后怎么清空里面的数据呢?
zzz小菜鸟zzz 2011-05-12
  • 打赏
  • 举报
回复
试一下
[Quote=引用 1 楼 drsmart 的回复:]

清空 你的adapter即可,
[/Quote]
DrSmart 2011-05-12
  • 打赏
  • 举报
回复
清空 你的adapter即可,

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧