参考APIDEMO:sdk\samples\android-19\content\LoaderCursor

1、创建主布局文件,里面只包含一个Fragment。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/FrameLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><fragmentandroid:id="@+id/fragment_list"android:name="com.example.android.content.loadercursor.CursorLoaderListFragment"android:layout_width="match_parent"android:layout_height="match_parent" /></FrameLayout>

2、创建主Activity文件中的android:name加载相应的Fragment

package com.example.android.content.loadercursor;import android.app.Activity;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SearchView;/*** The entry point to the CursorLoader sample. This sample demonstrates the use* of the {@link LoaderManager} to retrieve data from a {@link Cursor}. Here, a* list of contacts is displayed in a {@link ListFragment} and filtered using a* {@link SearchView} ActionBar item.*/
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

3、创建相应的Fragment

package com.example.android.content.loadercursor;import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;/*** A {@link ListFragment} that shows the use of a {@link LoaderManager} to* display a list of contacts accessed through a {@link Cursor}.*/
/** (1)继承Fragment或者其子类,用于创建一个Fragment。实现LoaderManager.LoaderCallbacks接口,用于与Loader的交互* 。 官方文档: A callback interface for a client to interact with the LoaderManager.* For example, you use the onCreateLoader() callback method to create a new* loader.*/
public class CursorLoaderListFragment extends ListFragment implementsLoaderManager.LoaderCallbacks<Cursor> {// This is the Adapter being used to display the list's data.SimpleCursorAdapter mAdapter;// The SearchView for doing filtering.SearchView mSearchView;/** (2)在Activity被创建时调用此方法。Called when the fragment's activity has been* created and this fragment's view hierarchy instantiated. You typically* initialize a Loader within the activity's onCreate() method, or within* the fragment's onActivityCreated() method.*/@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);// Give some text to display if there is no data. In a real// application this would come from a resource.setEmptyText("No phone numbers");/** Create an empty adapter we will use to display the loaded data. The* simple_list_item_2 layout contains two rows on top of each other* (text1 and text2) that will show the contact's name and status.*/// (3)设置Fragment的顯示內容mAdapter = new SimpleCursorAdapter(getActivity(),android.R.layout.simple_list_item_2, null, new String[] {Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },new int[] { android.R.id.text1, android.R.id.text2 }, 0);setListAdapter(mAdapter);// Start out with a progress indicator.setListShown(false);// Prepare the loader. Either re-connect with an existing one,// or start a new one./** (4)創建一個Loader,此Loader用于為Fragment載入內容。You typically initialize a* Loader within the activity's onCreate() method, or within the* fragment's onActivityCreated() method.* 此方法將自動調用LoaderManager.LoaderCallbacks接口的onCreateLoader方法。*/getLoaderManager().initLoader(0, null, this);}/*** An item has been clicked in the {@link ListView}. Display a toast with* the tapped item's id.*/@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {Toast.makeText(getActivity(), "Item clicked: " + id, Toast.LENGTH_LONG).show();}// These are the Contacts rows that we will retrieve.static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {Contacts._ID, Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS,Contacts.LOOKUP_KEY, };/** (5)Loader被創建時的操作,一般用于加載內容。In this example, the onCreateLoader() callback* method creates a CursorLoader. You must build the CursorLoader using its* constructor method, which requires the complete set of information needed* to perform a query to the ContentProvider.*/public Loader<Cursor> onCreateLoader(int id, Bundle args) {// This is called when a new Loader needs to be created. This// sample only has one Loader, so we don't care about the ID.// First, pick the base URI to use depending on whether we are// currently filtering.Uri baseUri;baseUri = Contacts.CONTENT_URI;// Now create and return a CursorLoader that will take care of// creating a Cursor for the data being displayed.String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("+ Contacts.DISPLAY_NAME + " != '' ))";String order = Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";return new CursorLoader(getActivity(), baseUri,CONTACTS_SUMMARY_PROJECTION, select, null, order);}/** (6)内容被加载完成后的操作。The loader will release the data once it knows the* application is no longer using it. For example, if the data is a cursor* from a CursorLoader, you should not call close() on it yourself. If the* cursor is being placed in a CursorAdapter, you should use the* swapCursor() method so that the old Cursor is not closed.*/public void onLoadFinished(Loader<Cursor> loader, Cursor data) {// Swap the new cursor in. (The framework will take care of closing the// old cursor once we return.)// Swap in a new Cursor, returning the old Cursor. Unlike// changeCursor(Cursor), the returned old Cursor is not closed.mAdapter.swapCursor(data);// The list should now be shown.if (isResumed()) {setListShown(true);} else {setListShownNoAnimation(true);}}// (7)Loader被重新加载时的操作。public void onLoaderReset(Loader<Cursor> loader) {// This is called when the last Cursor provided to onLoadFinished()// above is about to be closed. We need to make sure we are no// longer using it.mAdapter.swapCursor(null);}}

Loader之二:CursorLoader基本实例相关推荐

  1. solidworks api二次开发实例详解_Solidworks开发语言对比及分析

    很多初学Solidworks二次开发的同学,也许都会纠结使用何种语言进行二次开发.对于Solidworks二次开发的语言,官方有VBA,VB.NET,C#以及C++,四种语言. 用户通常会有如下疑问, ...

  2. python对abaqus本构二次开发_基于Python的Abaqus二次开发实例讲解

    第 1 页 共 11 页 基于 Python 的 Abaqus 二次开发实例讲解 ( asian58 2013.6.26 ) 基于 Python 的 Abaqus 的二次开发便捷之处在于: 1 .所有 ...

  3. python随机生成二维列表_对python产生随机的二维数组实例详解

    对python产生随机的二维数组实例详解 最近找遍了python的各个函数发现无法直接生成随机的二维数组,其中包括random()相关的各种方法,都没有得到想要的结果.最后在一篇博客中受到启发,通过列 ...

  4. oa java 二次开发_泛微OA ecology 二次开发实例 开发完整.doc

    泛微OA ecology 二次开发实例 开发完整 二次开发培训文档 ECOLOGY系统框架结构 主要的程序结构 Ecology Classbean 存放编译后的CLASS文件 js 系统中使用的JAV ...

  5. OpenSees二次开发实例02

    一个记录器的二次开发过程 前言 过程 结语 前言 本文介绍了一个源代码中附带的记录器的二次开发过程,记录器的h和cpp文件是源代码自带的.这次花费了我3天的时间,才实现了记录器的添加. 过程 本记录器 ...

  6. Python 语言 SAP2000 二次开发 实例

    目 录 Blog Links Remarks Example Blog Links DalNur | 博客总目录 Python语言 SAP2000 二次开发 -- 概述 Python语言 SAP200 ...

  7. lisp 圆柱螺旋线_Auto LISP对AutoCAD2002进行二次开发实例——绘制二维函数曲线

    Auto LISP 对 AutoCAD 2002 进行二次开发实例 ---绘制二维函数曲线Ξ李旭荣 ,任奕玲 ,梁秀英 ,刘梅英 (华中农业大学 工程技术学院 ,湖南 武汉 430070) 摘 要:主 ...

  8. python中的二维数组_Python中的二维数组实例(list与numpy.array)

    关于python中的二维数组,主要有list和numpy.array两种. 好吧,其实还有matrices,但它必须是2维的,而numpy arrays (ndarrays) 可以是多维的. 我们主要 ...

  9. python 二分类的实例_深入理解GBDT二分类算法

    我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习.深度学习.计算机视觉.智能对话系统相关内容, ...

  10. 基于python的abaqus二次开发_基于PYTHON的ABAQUS二次开发实例讲解

    第 1 页 共 11 页 基于 Python 的 Abaqus 二次开发实例讲解 ( asian58 2013.6.26 ) 基于 Python 的 Abaqus 的二次开发便捷之处在于: 1 .所有 ...

最新文章

  1. 阿里云 Redis 开发规范
  2. APUE(第九章)进程关系
  3. 九九乘法表用python怎么写_用python做个九九乘法表
  4. python-pdf添加水印
  5. 走进webpack(1)--环境拆分及模块化
  6. 使用镜像源安装EASY_INSTALL和PIP教程
  7. django-模板注释
  8. DE28 Matrix Methods for Inhomogeneous Systems
  9. Qt总结之十一:内存泄漏(汇总)
  10. Web Services的基本原理
  11. H5+APP安卓原生插件开发+离线打包
  12. Tomcat安装及配置教程(超详细的图文教程)
  13. 编译原理:CH3 语法分析
  14. 分享:一个Java写的教师信息管理系统(windowbuilder)(附码云源码)~~~
  15. FreeMarker下拉列表选中值回显
  16. NX文件名与工程图名自动关联
  17. 大年初一,给大家发红包了!
  18. Linux中的UID、GID和SID
  19. 获苹果中国区推荐,能带来多少新增
  20. 澳大利亚莫纳什大学推出公开漏洞奖励计划

热门文章

  1. 字符串关键字的散列映射 (25 分)【详细解析】
  2. 设有n 个程序{1,2,…, n }要存放在长度为L的磁带上。程序i存放在磁带上的长度是Li, 1<= i<= n。这n 个程序的读取概率分别是p1,p2,...,pn,且pi+p2+...+pn =
  3. 10行代码AC——UVA 11538-Chess Queen(数学规律+数列,附详细讲解)
  4. Python爬取某旅游网站中的中国城市信息
  5. 初识BGP外部网关协议(一)
  6. aem是什么意思_一台400匹的宽体RX7不装转子引擎,那装的是什么?
  7. 小程序加入人脸识别_人脸识别微信小程序案例:11.案例概述
  8. python温度转换代码分析_Python温度转换实例分析
  9. 内存溢出_关于PermGen Space内存溢出解决方案
  10. mysql文本自动递增_mysql-如何创建自动递增的字符串?