Best Way to Refresh Adapter/ListView on Android
来源:互联网 发布:非苹果电脑安装mac os 编辑:程序博客网 时间:2024/05/18 02:34
My book, "Hello Android" gives this as a way of using a custom db helper, setting up a cursor, and then setting up an adapter as follows:
Cursor cursorCustomDatabaseHelper test = new CustomDatabaseHelper(this);try { cursor = getData(); showData(cursor);} finally { test.close();}
With this however, everytime I need to refresh the data set, I need to keep running this block of code (which gets a bit difficult inside an onClick() for a button due to "this" not being available.
Is this the best way to refresh the data set, or should I look towards removing the .close and issue an adapter.notifyDataSetChanged()? If I do this, sometimes I get a force close as (and I can't remember at the moment) but sometimes it cannot Delete properly - I think this may be because the database is currently open and it tries to open again.
Should we also be declaring the variables for the Cursors, DatabaseHelpers and Adapter in the Class (outside of the OnCreate) so that they are accessible to all the functions?
I realise this is just poor programming at this stage, but Im trying to get some pointers as to the best way of doing things.
3 Answers
You should use adapter.notifyDataSetChanged()
.
simply add these code before setting Adapter it's working for me
listView.destroyDrawingCache();listView.setVisibility(ListView.INVISIBLE);listView.setVisibility(ListView.VISIBLE);
Following code works perfect for me
EfficientAdapter adp = (EfficientAdapter) QuickList.getAdapter();adp.UpdateDataList(EfficientAdapter.MY_DATA);adp.notifyDataSetChanged();QuickList.invalidateViews();QuickList.scrollBy(0, 0);
转载:http://stackoverflow.com/questions/4194124/best-way-to-refresh-adapter-listview-on-android
- Best Way to Refresh Adapter/ListView on Android
- Best way to learn android and java?
- Best way to pretty format json on console
- Android ListView自动加载 Android ListView pull up to refresh.
- Android ListView自动加载 Android ListView pull up to refresh.
- ListView pull to refresh
- 自定义ListView盘点-android-Ultra-Pull-to-Refresh(2)
- 自定义ListView盘点-Pull-to-Refresh.Rentals-Android(35)
- android pull to refresh listview 框架使用demo
- on the way to c++
- The best way to fix a bug
- The Best Way to Learn ASP.NET
- The best way to delete code
- The best way to handle the LazyInitializationException
- The best way to improve your listening
- What is the best way to calculate a checksum for a file that is on my machine?
- WampDeveloper Pro is the best (and fastest) way to create and run multiple websites on Windows, usin
- Android - What's the best way to share data between activities?
- iOS开源项目GitHub移动开发最受欢迎的开源项目
- 单例模式
- (Extjs)对于GridPanel的各种操作
- jdk与jre的区别
- 常用25个算法
- Best Way to Refresh Adapter/ListView on Android
- 关于时区 待续
- 文件流方式下载文件
- spring组件扫描<context:component-scan/>使用详解
- POJ 1442 Black Box(堆||treap)
- 自然语言和机器学习笔记
- Google Maps Android API v2 (google Maps api key 申请)
- do{...}while(0)的意义和用法
- JSP页面给中编写java代码时"编译报错