Loader之二:CursorLoader基本实例 分类: H1_ANDROID 2013-11-16 10:50 5447人阅读 评论(0) 收藏
参考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基本实例 分类: H1_ANDROID 2013-11-16 10:50 5447人阅读 评论(0) 收藏的更多相关文章
- 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏
RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...
- 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏
文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...
- C#多线程(上) 分类: C# 线程 2015-03-09 10:35 174人阅读 评论(0) 收藏
一.多线程的相关概念 什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源. 而一个进程又是由多个线程所组成的. 什么是线程? 线程是程序中的一个执行 ...
- __int64 与long long 的区别 分类: Brush Mode 2014-08-14 10:22 64人阅读 评论(0) 收藏
//为了和DSP兼容,TSint64和TUint64设置成TSint40和TUint40一样的数 //结果VC中还是认为是32位的,显然不合适 //typedef signed long int ...
- C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏
1. 概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...
- 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 分类: dp 2015-05-21 10:50 14人阅读 评论(0) 收藏
Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并 ...
- 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏
也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...
- 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏
1.HTML部分: <form id="form1" runat="server"> <script src=".. ...
- C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏
using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...
随机推荐
- HDU 3487(Play with Chain-Splay)[template:Splay]
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 数据库中解析XML
简介:OPENXML方法使用一例实现导入功能 DECLARE @strProjGUID AS VARCHAR(50) DECLARE @strProjCode AS VARCHAR(50) DEC ...
- IPod在Linux下的实战
刚收到一个朋友送的Ipod,经过研究今天我为大家分享一点在Linux系统下使用的经验.Apple的iPod它炫目时尚,超薄还可以俘储大量歌曲,这使得iPod十分流行.但流行的同时也带来了一些问题, 在 ...
- c#的中英文混合字符串截取
public class StringHelper { public static string GetSubString(string str, int len) ...
- gdbserver 使用方法
1.分别编译出在宿主机运行的交叉调试器arm-linux-gdb和在目标板上运行的gdbserver: 2.在目标板开启gdbserver#gdbserver 宿主机ip:任意指定端口号 ./待调试 ...
- COGS 163 [USACO Mat07] 牛语
COGS 163 [USACO Mat07] 牛语 输入文件:latin.in 输出文件:latin.out 简单对比 时间限制:1 s 内存限制:128 MB 奶牛们听说猪发明了一种秘密 ...
- Android中Alarm的机制
本次给大家分析的是Android中Alarm的机制所用源码为最新的Android4.4.4.首先简单介绍如何使用Alarm并给出其工作原理,接着分析Alarm和Timer以及Handler在完成定时任 ...
- 21.Spring Boot 使用Java代码创建Bean并注册到Spring中
转自:https://blog.csdn.net/catoop/article/details/50558333
- HTTP请求报文、响应报文
HTTP请求报文 HTTP请求报文由3部分组成(请求行+请求头+请求体): 请求行:①是请求方法,GET和POST是最常见的HTTP方法,除此以外还包括DELETE.HEAD.OPTIONS.PUT. ...
- react取消监听scroll事件
如果要移除事件addEventListener的执行函数必须使用外部函数而不能直接使用匿名函数 错误写法: // 这样写是移除不了滚动事件的 componentDidMount() { // 添加滚动 ...