Best Way to Refresh Adapter/ListView on Android
2013-06-19 10:19
417 查看
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:
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.
You should use
simply add these code before setting Adapter it's working for me
Following code works perfect for me
转载:http://stackoverflow.com/questions/4194124/best-way-to-refresh-adapter-listview-on-android
Cursor cursor CustomDatabaseHelper 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
相关文章推荐
- Android--PullToRefreshListView的onRefreshComplete()不起作用的问题
- android PullToRefreshListView(listview) baseadapter getview 循环调用
- android listvieew PullToRefreshListView设置setOnTouchListener()失效
- Android 使用PullToRefreshExpandableListView不能setAdapter的问题
- android PullToRefreshListView onRefreshComplete 无效问题
- Android-PullToRefresh onRefreshComplete()失效问题
- Android开发学习笔记十七 PullToRefreshListView
- listView利用BaseAdapter及异步任务Gson解析,使用Picasso网络获取资源进行图文混排,PullToRefresh上拉刷新下拉加载
- 【转载】 Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
- Android 使用PullToRefresh实现下拉刷新和上拉加载(ExpandableListView)
- Android--PullToRefreshListView 的简单使用
- Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
- Android项目:使用pulltorefresh开源项目扩展为下拉刷新上拉加载更多的处理方法,监听listview滚动方向
- Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
- Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
- (4.2.7)【android开源组件】Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
- The Best Way to Unit Test in Android
- 采用github上的开源项目Android-PullToRefresh实现ListView的下拉刷新和上拉加载
- Android项目:使用pulltorefresh开源项目扩展为下拉刷新上拉加载更多的处理方法,监听listview滚动方向 推荐
- Android 去掉ListView/GridView下拉出空白回弹的效果,以及使用PullToRefresh出现的问题