利用notifyDataSetChanged()方法实现启动活动之后更新 ListView
来源:互联网 发布:宜人贷淘宝验证不了 编辑:程序博客网 时间:2024/06/05 16:53
启动活动之后更新 ListView
使用 ListView 的时候,在活动启动之后,如果 ListView 的数据有一些变更,该如何将变更之后的数据显示在界面上,要用到 adapter 的 notifyDataSetChanged() 方法,但是在数据改变之后直接 adapter.notifyDataSetChanged(); 却是更新不出来的,必须要修改一些地方,先清除 list 中的数据,再加载更新后的数据,在使用这个方法。
private List<???> list = new ArrayList<>();private ListView listView = (ListView)findViewById(R.id.listView);private ListAdapter adapter = null;if (list.size() == 0) { //加载所有 ****** adapter = new ListAdapter(list,???); listView.setAdapter(adapter);} else { //加载变更之后的数据 list.clear(); //先清除 List 中的数据 list.addAll(***); //然后在加载更新后的所有的数据 adapter.notifyDataSetChanged(); //更新数据}
为什么不能直接用哪个方法?第一次 adapter 加载 list 的时候 adapter 保存的 list 指向的是一开始 List 的引用,在更新了数据之后,list 指向了新的 List 引用,但是 adapter 保存的 list 指向的还是原来的引用,因此在调用 notifyDataSetChanged() 时,adapter 会跟据原来的引用去更新,因此当然还是原来的结果,不会进行更新。进行以上操作之后, adapter 保存的是指向的是新的 List 的引用,就更新了。
阅读全文
0 0
- 利用notifyDataSetChanged()方法实现启动活动之后更新 ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- Android——Listview不用notifydatasetchanged更新数据的方法
- ListView局部更新Item,不调用notifyDataSetChanged()方法
- Android列表ListView用NotifyDataSetChanged实现动态更新
- 调用notifyDataSetChanged无法更新ListView
- CreateEvent 自动复位和手动复位的区别
- 【Swift】 Collections初识
- jQuery对象和javascript的对象的转化
- spring原理
- 开启动画用start
- 利用notifyDataSetChanged()方法实现启动活动之后更新 ListView
- GitHub学习之旅 之 HelloWorld
- Codeforces Gym 101190 (NEERC 2016) F. Foreign Postcards (dp + 期望)
- 【pg_rman】备份GIN索引bug
- 数据结构 最小生成树 Kruskal算法
- python环境变量设置
- Codeforces Gym 101190 (NEERC 2016) H. Hard Refactoring (模拟 + 树状数组)
- 乱序字符串
- 数据结构 最小生成树 Prim算法