LoaderManager使用详解(二)---了解LoaderManager
LoaderManager是什么?
实现LoaderManager.LoaderCallbacks<D>接口
public class SampleActivity extends Activity implements LoaderManager.LoaderCallbacks<D> {public Loader<D> onCreateLoader(int id, Bundle args) { ... }public void onLoadFinished(Loader<D> loader, D data) { ... }public void onLoaderReset(Loader<D> loader) { ... }/* ... */ }
从Managed Cursor转移到LoaderManager
public class SampleListActivity extends ListActivity implementsLoaderManager.LoaderCallbacks<Cursor> {private static final String[] PROJECTION = new String[] { "_id", "text_column" };// The loader's unique id. Loader ids are specific to the Activity or// Fragment in which they reside.private static final int LOADER_ID = 1;// The callbacks through which we will interact with the LoaderManager.private LoaderManager.LoaderCallbacks<Cursor> mCallbacks;// The adapter that binds our data to the ListViewprivate SimpleCursorAdapter mAdapter;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);String[] dataColumns = { "text_column" };int[] viewIDs = { R.id.text_view };// Initialize the adapter. Note that we pass a 'null' Cursor as the// third argument. We will pass the adapter a Cursor only when the// data has finished loading for the first time (i.e. when the// LoaderManager delivers the data to onLoadFinished). Also note// that we have passed the '0' flag as the last argument. This// prevents the adapter from registering a ContentObserver for the// Cursor (the CursorLoader will do this for us!).mAdapter = new SimpleCursorAdapter(this, R.layout.list_item,null, dataColumns, viewIDs, 0);// Associate the (now empty) adapter with the ListView.setListAdapter(mAdapter);// The Activity (which implements the LoaderCallbacks<Cursor>// interface) is the callbacks object through which we will interact// with the LoaderManager. The LoaderManager uses this object to// instantiate the Loader and to notify the client when data is made// available/unavailable.mCallbacks = this;// Initialize the Loader with id '1' and callbacks 'mCallbacks'.// If the loader doesn't already exist, one is created. Otherwise,// the already created Loader is reused. In either case, the// LoaderManager will manage the Loader across the Activity/Fragment// lifecycle, will receive any new loads once they have completed,// and will report this new data back to the 'mCallbacks' object.LoaderManager lm = getLoaderManager();lm.initLoader(LOADER_ID, null, mCallbacks);}@Overridepublic Loader<Cursor> onCreateLoader(int id, Bundle args) {// Create a new CursorLoader with the following query parameters.return new CursorLoader(SampleListActivity.this, CONTENT_URI,PROJECTION, null, null, null);}@Overridepublic void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {// A switch-case is useful when dealing with multiple Loaders/IDsswitch (loader.getId()) {case LOADER_ID:// The asynchronous load is complete and the data// is now available for use. Only now can we associate// the queried Cursor with the SimpleCursorAdapter.mAdapter.swapCursor(cursor);break;}// The listview now displays the queried data.}@Overridepublic void onLoaderReset(Loader<Cursor> loader) {// For whatever reason, the Loader's data is now unavailable.// Remove any references to the old data by replacing it with// a null Cursor.mAdapter.swapCursor(null);} }
总结
转载于:https://www.cnblogs.com/android-blogs/p/4962508.html
LoaderManager使用详解(二)---了解LoaderManager相关推荐
- Android Loader 异步加载详解二:探寻Loader内部机制
Android Loader 异步加载详解二:探寻Loader内部机制 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/7025991 ...
- 安卓 linux init.rc,[原创]Android init.rc文件解析过程详解(二)
Android init.rc文件解析过程详解(二) 3.parse_new_section代码如下: void parse_new_section(struct parse_state *state ...
- [转]文件IO详解(二)---文件描述符(fd)和inode号的关系
原文:https://www.cnblogs.com/frank-yxs/p/5925563.html 文件IO详解(二)---文件描述符(fd)和inode号的关系 ---------------- ...
- PopUpWindow使用详解(二)——进阶及答疑
相关文章: 1.<PopUpWindow使用详解(一)--基本使用> 2.<PopUpWindow使用详解(二)--进阶及答疑> 上篇为大家基本讲述了有关PopupWindow ...
- Android init.rc文件解析过程详解(二)
Android init.rc文件解析过程详解(二) 3.parse_new_section代码如下: void parse_new_section(struct parse_state *state ...
- linux 进程间通信 dbus-glib【实例】详解二(下) 消息和消息总线(ListActivatableNames和服务器的自动启动)(附代码)
linux 进程间通信 dbus-glib[实例]详解一(附代码)(d-feet工具使用) linux 进程间通信 dbus-glib[实例]详解二(上) 消息和消息总线(附代码) linux 进程间 ...
- linux 进程间通信 dbus-glib【实例】详解二(上) 消息和消息总线(附代码)
linux 进程间通信 dbus-glib[实例]详解一(附代码)(d-feet工具使用) linux 进程间通信 dbus-glib[实例]详解二(上) 消息和消息总线(附代码) linux 进程间 ...
- Android Gradle 自定义Task详解二:进阶
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78523958 本文出自[赵彦军的博客] 系列目录 Android Gradle使用 ...
- EXT核心API详解(二)-Array/Date/Function/Number/String
EXT核心API详解(二)-Array/Date/Function/Number/String Array类 indexOf( Object o ) Number object是否在数组中,找不到返 ...
- OS--进程间通信详解(二)
OS–进程间通信详解(二) 文章目录 OS--进程间通信详解(二) 一.进程间通信 1.互斥量 Futexes Pthreads中的互斥量 2.管程 3.消息传递 消息传递系统的设计要点 用消息传递解 ...
最新文章
- .net core i上 K8S(二)运行简单.netcore程序
- 阿里云亮相2019联通合作伙伴大会,边缘计算等3款云产品助力5G时代产业数字化转型...
- Java虚拟机学习(5):内存调优
- PHP5魔术函数与魔术常量
- 【269】蓝牙键盘连接
- WinForm部署问题
- javascript 懒加载技术(lazyload)简单实现
- 算法与数据结构 第2章 排序基础 下 插入排序
- Unity3d 2019室内光照贴图lightmap快速烘焙(转载)
- windows 7 多国语言包官方下载资源共享(32/64位)
- 锡兰1.1.0现已上市
- 小程序封装请求工具http.js
- SAP开发入门-ABAP
- 【OVS2.5.0源码分析】sFlow实现分析(3)
- 微信公众号文章如何排版?
- 数字IC设计 - 逻辑综合简介与Design Compiler使用(GUI方式)
- Ubuntu卡在登陆界面循环
- Ubuntu中文乱码的解决方案
- php短信炸弹,php发送短信炸弹
- App/H5/小程序测试技术
热门文章
- 中小企业CRM评测-八百客
- TCP三次握手的原理及***手段
- Flink SQL Client注册SCALA UDF完整流程
- Kaggle竞赛方案分享:如何分辨杂草和植物幼苗(转)
- git lfs的安装和使用详细案例
- DataFrame纵向合并
- RHEL7恢复.bashrc文件
- 列举mysql的所有触发器以及删除触发器
- Clion this file does not belong to any project target问题解决方案
- java file类复制文件路径_java进阶(34)--File类、目录复制