How to clear the views which are held in the ListView's RecycleBin?
2013-07-26 16:33
537 查看
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) :
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是作为全局变量的,所以我是这样使用的:
adDeviceList.setList(MainService.mainService.nowResult);
lvDevices.setAdapter(adDeviceList);
最后发现还是没有效果
最后我把2种方法放在一起,居然解决了...看来这2个都要设置才可以清除缓存
lvDevices.invalidateViews();
adDeviceList.setList(MainService.mainService.nowResult);
lvDevices.setAdapter(adDeviceList);
而我找了很久,都没有发现可以清除缓存的方法。有人说用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) :
@Override public 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 config the tftpd-hpa server in the ubuntu using for remote tftp client '-p' and '-r' command
- "The file you are trying to open, '[filename]', is in a different format" Excel Error
- How to get the content of the 'identity' section in web.config
- 92 Examine the output of the query that you executed to list the objects in the recycle bin: SQL> SE
- How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0
- [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq
- How to hide the "Repair" option in the .NET installation package
- How to solve "one or more projects in the solution were not loaded correctly" problem?
- [IOS] 错误:Cannot assign to 'self' outside of a method in the init family
- How to delete all email in the mail server using telnet & WCScript - 如何用 Telnet 删除邮件服务器上的所有邮件
- There are inconsistent line endings in the 'xxx' script. Some are Mac OS X (UNIX) and some are Windows
- Google to Android Developers: 'Do Not Develop in the Open'
- How to share text to Message in the app you are building(iOS platform)
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.
- How to solve problem caused when the CRT library and MFC libraries are linked in the wrong order
- in the prefix, you've chosen, there are no KDE headers installed.
- How to resolve '_DllMain@12 already defined in xxx.obj' ?
- How to fix Rosetta stone error 2123 -- "This was an error in the application"
- How to determine which checkboxes are checked in postback (sqlstr="delete spprod where id in ("&request("sid")&")")
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.