Loader之二:CursorLoader基本实例
参考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" > <fragment
android: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 { @Override
protected 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 implements
LoaderManager.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.
*/@Override
public 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.
*/
@Override
public 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基本实例的更多相关文章
- 5、SAMBA服务二:配置实例
①:SAMBA服务一:参数详解 ②:SAMBA服务二:配置实例 5.2.3.Samba共享目录配置实例 1.允许匿名用户读取/it共享目录,修改/etc/samba/smb.conf,在最后添加以下内 ...
- Loader之二:CursorLoader基本实例 分类: H1_ANDROID 2013-11-16 10:50 5447人阅读 评论(0) 收藏
参考APIDEMO:sdk\samples\android-19\content\LoaderCursor 1.创建主布局文件,里面只包含一个Fragment. <FrameLayout xml ...
- tkprof工具详解二(一些实例)
TKPROF是一个可执行文件,自带在Oracle Server软件中,无需额外的安装. 该工具文件可以用来解析ORACLE的SQL TRACE(10046) 以便生成更可读的内容. 实际上tkpro ...
- Activiti工作流学习(二)流程实例、执行对象、任务
一.前言 前面说明了基本的流程部署.定义,启动流程实例等基本操作,下面我们继续来学习流程实例.执行对象.任务. 二.流程实例.执行对象说明 整个Activiti的生命周期经过了如下的几个步骤: 1.流 ...
- mybatis 详解(二)------入门实例(基于XML)
通过上一小节,mybatis 和 jdbc 的区别:http://www.cnblogs.com/ysocean/p/7271600.html,我们对 mybatis有了一个大致的了解,下面我们通过一 ...
- 记录下项目中常用到的JavaScript/JQuery代码二(大量实例)
记录下项目中常用到的JavaScript/JQuery代码一(大量实例) 1.input输入框监听变化 <input type="text" style="widt ...
- Laravel5中通过SimpleQrCode扩展包生成二维码实例
Simple Qrcode是基于强大的Bacon/BaconQrCode库开发的针对Laravel框架的封装版本,用于在Laravel中为生成二维码提供接口. 安装SimpleQrCode扩展包 在项 ...
- PHPCMS V9 模块开发 二次开发实例 留言本
鄙人实现了PHPCMS V9 产品开发权威指南(2011官方最新版).doc中的留言板实例,并加上模块安装和卸载功能, 程序可以运行,但只实现基本功能,目的是想让和我一样徘徊在PHPCMS门口不知道从 ...
- python 面向对象二 类和实例
一.类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法, ...
随机推荐
- [置顶] 【C/C++学习】之十三、虚函数剖析
所谓虚函数,虚就虚在“推迟联编”或者“动态联编”上,一个类函数的调用并不是在编译时刻被确定的,而是在运行时刻被确定的.由于编写代码的时候并不能确定被调用的是基类的函数还是哪个派生类的函数,所以被称为“ ...
- easyui的combobox将得到的数据设定为下拉框默认值和复选框设定默认值
通过easyui做了一个表,表里是从数据库拿到的数据. 现在双击某一行,通过点击行的id取到这一行的所有数据,现在需要修改这些得到的数据, 其中部分数据是<select>这个选择的, 问题 ...
- java多线程什么时候释放锁—wait()、notify()
由于等待一个锁定线程只有在获得这把锁之后,才能恢复运行,所以让持有锁的线程在不需要锁的时候及时释放锁是很重要的.在以下情况下,持有锁的线程会释放锁: 1. 执行完同步代码块. 2. 在执行 ...
- php get_ini 和 get_cfg_var 的区别
get_ini 和 get_cfg_var 都是用来获取 php 配置信息的函数. 区别是 get_ini 是用来获取当前运行的配置信息,get_cfg_var 是用来获取配置文件(php.ini)的 ...
- pubwin会员合并
此博文已移至爬不稳独立博客:www.pubwin2009.net连接:http://www.pubwin2009.net/index.php/post/15.html 我们说下过程(这里,我们要求两个 ...
- js 图片无缝循环
<html> <head> <title>Js图片无缝滚动</title> <style type="text/css"> ...
- 使用Qt编写服务器端程序(包括Http传输服务器端)的方法
使用Qt编写客户端的程序的示例或demo较多,但是编写服务器端程序的demo很少.当然,服务器端的程序一般不需要带界面,这点我们可以理解.不过有些时候我们还是需要使用Qt编写一个简单的测试用的服务器代 ...
- Unix/Linux环境C编程入门教程(37) shell常用命令演练
cat命令 cat命令可以用来查看文件内容. cat [参数] 文件名. grep-指定文件中搜索指定字符内容. Linux的目录或文件. -path '字串' 查找路径名匹配所给字串的所有文件 ...
- k-d Tree in TripAdvisor
Today, TripAdvisor held a tech talk in Columbia University. The topic is about k-d Tree implemented ...
- LeeCode-Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...