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

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

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/FrameLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context=".MainActivity" >
  11.  
  12. <fragment
  13. android:id="@+id/fragment_list"
  14. android:name="com.example.android.content.loadercursor.CursorLoaderListFragment"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent" />
  17.  
  18. </FrameLayout>

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

  1. package com.example.android.content.loadercursor;
  2.  
  3. import android.app.Activity;
  4. import android.app.ListFragment;
  5. import android.app.LoaderManager;
  6. import android.database.Cursor;
  7. import android.os.Bundle;
  8. import android.widget.SearchView;
  9.  
  10. /**
  11. * The entry point to the CursorLoader sample. This sample demonstrates the use
  12. * of the {@link LoaderManager} to retrieve data from a {@link Cursor}. Here, a
  13. * list of contacts is displayed in a {@link ListFragment} and filtered using a
  14. * {@link SearchView} ActionBar item.
  15. */
  16. public class MainActivity extends Activity {
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. }
  23.  
  24. }

3、创建相应的Fragment

  1. package com.example.android.content.loadercursor;
  2.  
  3. import android.app.ListFragment;
  4. import android.app.LoaderManager;
  5. import android.content.CursorLoader;
  6. import android.content.Loader;
  7. import android.database.Cursor;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.provider.ContactsContract.Contacts;
  11. import android.view.View;
  12. import android.widget.ListView;
  13. import android.widget.SearchView;
  14. import android.widget.SimpleCursorAdapter;
  15. import android.widget.Toast;
  16.  
  17. /**
  18. * A {@link ListFragment} that shows the use of a {@link LoaderManager} to
  19. * display a list of contacts accessed through a {@link Cursor}.
  20. */
  21. /*
  22. * (1)继承Fragment或者其子类,用于创建一个Fragment。实现LoaderManager.LoaderCallbacks接口,用于与Loader的交互
  23. * 。 官方文档: A callback interface for a client to interact with the LoaderManager.
  24. * For example, you use the onCreateLoader() callback method to create a new
  25. * loader.
  26. */
  27. public class CursorLoaderListFragment extends ListFragment implements
  28. LoaderManager.LoaderCallbacks<Cursor> {
  29.  
  30. // This is the Adapter being used to display the list's data.
  31. SimpleCursorAdapter mAdapter;
  32.  
  33. // The SearchView for doing filtering.
  34. SearchView mSearchView;
  35.  
  36. /*
  37. * (2)在Activity被创建时调用此方法。Called when the fragment's activity has been
  38. * created and this fragment's view hierarchy instantiated. You typically
  39. * initialize a Loader within the activity's onCreate() method, or within
  40. * the fragment's onActivityCreated() method.
  41. */@Override
  42. public void onActivityCreated(Bundle savedInstanceState) {
  43. super.onActivityCreated(savedInstanceState);
  44.  
  45. // Give some text to display if there is no data. In a real
  46. // application this would come from a resource.
  47. setEmptyText("No phone numbers");
  48.  
  49. /*
  50. * Create an empty adapter we will use to display the loaded data. The
  51. * simple_list_item_2 layout contains two rows on top of each other
  52. * (text1 and text2) that will show the contact's name and status.
  53. */
  54. // (3)设置Fragment的顯示內容
  55. mAdapter = new SimpleCursorAdapter(getActivity(),
  56. android.R.layout.simple_list_item_2, null, new String[] {
  57. Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },
  58. new int[] { android.R.id.text1, android.R.id.text2 }, 0);
  59. setListAdapter(mAdapter);
  60.  
  61. // Start out with a progress indicator.
  62. setListShown(false);
  63.  
  64. // Prepare the loader. Either re-connect with an existing one,
  65. // or start a new one.
  66. /*
  67. * (4)創建一個Loader,此Loader用于為Fragment載入內容。You typically initialize a
  68. * Loader within the activity's onCreate() method, or within the
  69. * fragment's onActivityCreated() method.
  70. * 此方法將自動調用LoaderManager.LoaderCallbacks接口的onCreateLoader方法。
  71. */
  72. getLoaderManager().initLoader(0, null, this);
  73.  
  74. }
  75.  
  76. /**
  77. * An item has been clicked in the {@link ListView}. Display a toast with
  78. * the tapped item's id.
  79. */
  80. @Override
  81. public void onListItemClick(ListView l, View v, int position, long id) {
  82. Toast.makeText(getActivity(), "Item clicked: " + id, Toast.LENGTH_LONG)
  83. .show();
  84. }
  85.  
  86. // These are the Contacts rows that we will retrieve.
  87. static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
  88. Contacts._ID, Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS,
  89. Contacts.LOOKUP_KEY, };
  90.  
  91. /*
  92. * (5)Loader被創建時的操作,一般用于加載內容。In this example, the onCreateLoader() callback
  93. * method creates a CursorLoader. You must build the CursorLoader using its
  94. * constructor method, which requires the complete set of information needed
  95. * to perform a query to the ContentProvider.
  96. */public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  97. // This is called when a new Loader needs to be created. This
  98. // sample only has one Loader, so we don't care about the ID.
  99. // First, pick the base URI to use depending on whether we are
  100. // currently filtering.
  101. Uri baseUri;
  102. baseUri = Contacts.CONTENT_URI;
  103.  
  104. // Now create and return a CursorLoader that will take care of
  105. // creating a Cursor for the data being displayed.
  106. String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
  107. + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
  108. + Contacts.DISPLAY_NAME + " != '' ))";
  109. String order = Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
  110.  
  111. return new CursorLoader(getActivity(), baseUri,
  112. CONTACTS_SUMMARY_PROJECTION, select, null, order);
  113. }
  114.  
  115. /*
  116. * (6)内容被加载完成后的操作。The loader will release the data once it knows the
  117. * application is no longer using it. For example, if the data is a cursor
  118. * from a CursorLoader, you should not call close() on it yourself. If the
  119. * cursor is being placed in a CursorAdapter, you should use the
  120. * swapCursor() method so that the old Cursor is not closed.
  121. */public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
  122. // Swap the new cursor in. (The framework will take care of closing the
  123. // old cursor once we return.)
  124. // Swap in a new Cursor, returning the old Cursor. Unlike
  125. // changeCursor(Cursor), the returned old Cursor is not closed.
  126. mAdapter.swapCursor(data);
  127.  
  128. // The list should now be shown.
  129. if (isResumed()) {
  130. setListShown(true);
  131. } else {
  132. setListShownNoAnimation(true);
  133. }
  134. }
  135.  
  136. // (7)Loader被重新加载时的操作。
  137. public void onLoaderReset(Loader<Cursor> loader) {
  138. // This is called when the last Cursor provided to onLoadFinished()
  139. // above is about to be closed. We need to make sure we are no
  140. // longer using it.
  141. mAdapter.swapCursor(null);
  142. }
  143.  
  144. }

Loader之二:CursorLoader基本实例的更多相关文章

  1. 5、SAMBA服务二:配置实例

    ①:SAMBA服务一:参数详解 ②:SAMBA服务二:配置实例 5.2.3.Samba共享目录配置实例 1.允许匿名用户读取/it共享目录,修改/etc/samba/smb.conf,在最后添加以下内 ...

  2. Loader之二:CursorLoader基本实例 分类: H1_ANDROID 2013-11-16 10:50 5447人阅读 评论(0) 收藏

    参考APIDEMO:sdk\samples\android-19\content\LoaderCursor 1.创建主布局文件,里面只包含一个Fragment. <FrameLayout xml ...

  3. tkprof工具详解二(一些实例)

    TKPROF是一个可执行文件,自带在Oracle Server软件中,无需额外的安装. 该工具文件可以用来解析ORACLE的SQL TRACE(10046) 以便生成更可读的内容.  实际上tkpro ...

  4. Activiti工作流学习(二)流程实例、执行对象、任务

    一.前言 前面说明了基本的流程部署.定义,启动流程实例等基本操作,下面我们继续来学习流程实例.执行对象.任务. 二.流程实例.执行对象说明 整个Activiti的生命周期经过了如下的几个步骤: 1.流 ...

  5. mybatis 详解(二)------入门实例(基于XML)

    通过上一小节,mybatis 和 jdbc 的区别:http://www.cnblogs.com/ysocean/p/7271600.html,我们对 mybatis有了一个大致的了解,下面我们通过一 ...

  6. 记录下项目中常用到的JavaScript/JQuery代码二(大量实例)

    记录下项目中常用到的JavaScript/JQuery代码一(大量实例) 1.input输入框监听变化 <input type="text" style="widt ...

  7. Laravel5中通过SimpleQrCode扩展包生成二维码实例

    Simple Qrcode是基于强大的Bacon/BaconQrCode库开发的针对Laravel框架的封装版本,用于在Laravel中为生成二维码提供接口. 安装SimpleQrCode扩展包 在项 ...

  8. PHPCMS V9 模块开发 二次开发实例 留言本

    鄙人实现了PHPCMS V9 产品开发权威指南(2011官方最新版).doc中的留言板实例,并加上模块安装和卸载功能, 程序可以运行,但只实现基本功能,目的是想让和我一样徘徊在PHPCMS门口不知道从 ...

  9. python 面向对象二 类和实例

    一.类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法, ...

随机推荐

  1. (转) launch failed.Binary not found in Linux/Ubuntu解决方案

    原地址: http://blog.csdn.net/abcjennifer/article/details/7573916 Linux下出现launch failed.Binary not found ...

  2. JDBC学习入门

    一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...

  3. Js之Screen对象

    Window Screen window.screen 对象在编写时可以不使用 window 这个前缀. 属性: screen.availWidth - 可用的屏幕宽度,以像素计,减去界面特性,比如窗 ...

  4. php-app开发接口加密

    自己平时工作中用到的一套接口加密规则,记录下来以后用: /** 2 inc 3 解析接口 客户端接口传输规则: 1.用cmd参数(base64)来动态调用不同的接口,接口地址统一为 http://a. ...

  5. 从C++到Qt(命令行编译,讲解原理)

    Qt 是 C++ 的库,Qt 在 ansi C++ 的基础上进行了一点扩展. 但国内似乎比较浮躁,学Qt的很多连基本的C++如何编译似乎都不太清楚.本文舍弃IDE或qmake.cmake等工具的束缚, ...

  6. CentOS安装错误:no default or ui configuration

    靠,以后再也不用浏览器自带的下载工具下载镜像文件了,原来是下载的不完整,但是显示完全下载完毕了,真特么误导人.文件的checksum不对. references: https://www.centos ...

  7. C语言的本质(37)——makefile之隐含规则和模式规则

    Makefile有很多灵活的写法,可以写得更简洁,同时减少出错的可能.本节我们来看看这样一个例子还有哪些改进的余地. 一个目标依赖的所有条件不一定非得写在一条规则中,也可以拆开写,例如: main.o ...

  8. 2014第一周五开发问题记URL传参乱码等

    今天修改了页面中URL传中文参数乱码问题,本来远离通过在tomcat中配置URIEncoder是可以解决所有乱码问题的,但怕以后有人下载一个新的tomcat然后直接把程序放里面运行然后再发现乱码问题而 ...

  9. linux常用查看硬件设备信息命令(转载)

    系统 # uname -a                                       # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue         ...

  10. FZU1862(线段树 或者 DP)

    Problem 1862 QueryProblem Accept: 100    Submit: 249Time Limit: 2000 mSec    Memory Limit : 32768 KB ...