How to clear the views which are held in the ListView's RecycleBin?
来源:互联网 发布:多角度 目标检测算法 编辑:程序博客网 时间:2024/05/25 19:58
android 的listview 控件,我自己使用了converview来进行缓存设置(不懂什么是convertview的请自行百度),但是发现在某些情况下会有问题。具体而言,我的问题是这样的:我需要点击一下listview的某一项,此时需要更新数据,让listview显示新的数据。而此时,由于缓存的设置,会导致更新并没有办法显示,这个时候调用notifyDataSetChanged是没有效果的。所以我们需要将缓存清除。
而我找了很久,都没有发现可以清除缓存的方法。有人说用invalidate方法,但是经过测试也没有用。
这个时候看到有人说重新设置一下adapter就可以清除缓存了,发现还是不行
Calling invalidate() or invalidateViews() did not do the trick for me (as mentioned in the correct answer). The recycled views were still stored in the ListView. I had to dig in Android source code to find a solution. I checked many methods, including the setAdapter() method of the ListView class (Android API 15) :
@Overridepublic void setAdapter(ListAdapter adapter) { // ... mRecycler.clear(); // ...}
As you noticed, setting an adapter clears the recycler, which holds all the recycled views in a list view. You do not have to create a new adapter, setting the same adapter is enough to clear the recycled views list in the list view :
Adapter adapter = listview.getAdapter ();// ... Modify adapter ... do anything else you need to do// To clear the recycled views list :listview.setAdapter ( adapter );
在我的项目中,adapter是作为全局变量的,所以我是这样使用的:adDeviceList.setList(MainService.mainService.nowResult);
lvDevices.setAdapter(adDeviceList);
最后发现还是没有效果
最后我把2种方法放在一起,居然解决了...看来这2个都要设置才可以清除缓存
lvDevices.invalidateViews();
adDeviceList.setList(MainService.mainService.nowResult);
lvDevices.setAdapter(adDeviceList);
- How to clear the views which are held in the ListView's RecycleBin?
- How to clear the grid
- (NOT CLEAR)Question 45: Which of the following options are returned by the typeid operator in C++?
- given two directory trees how to find which files are the same?
- How to clear the archive log
- How to programmatically clear the filesystem memory cache in C++ on a Linux system?
- How Rich Are You in the World?
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.
- (NOT CLEAR)Question 18: In the given C++ code snippet, which of the following statements correctly identify how Mon of enum DOW
- How to share text to Message in the app you are building(iOS platform)
- Which of the following expression(s) will NOT cause an error when used to replace the ***** in the C
- How to: Filter List Views on the UI Specific Level
- How to find child controls that are located in the template of a parent control
- Clear Blogging: How People Blogging Are Changing the World and How You Can Join Them
- BroadcastReceiver基础总结
- 数据结构排序系列详解之四 快速排序
- 讨论:Hadoop社区与中国云计算开源展望
- java web filter实现浏览器不缓存页面
- 使用Eclipse RCP进行桌面程序开发(六):向OpenGL进军
- How to clear the views which are held in the ListView's RecycleBin?
- 初始化一个构建环境
- 统计源代码行数
- poj 2186——Popular Cows
- Eclipse RCP 开发:从登录对话框说开去
- json字符串转换为JSONObject和JSONArray
- jquery检测手机,邮箱代码
- 1093 A+B for Input-Output Practice (V)
- Cursor使用过程中遇到的一个问题