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 ...
随机推荐
- iOS_31_cocos2d_微信飞机
终于效果图: 纹理素材 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400 ...
- android图像处理系列之六--给图片添加边框(下)-图片叠加
前面介绍了一种用透明图片叠加的方式添加花边边框,下面将介绍另外一种图片叠加添加花边边框的方式.前面方法有一个缺点,就是做成PNG图片,图片体积会很大,不是一般的大,比同样的JPG大三倍多,如果项目可以 ...
- thinkphp内置标签简单讲解
thinkphp内置标签简单讲解 1.volist循环 name 需要遍历的数据 id 类似于foreach中 value offset 截取数据起始位置 length 截取数据的个数 mod 奇偶数 ...
- JS match方法的返回数据的探究
match方法是JS的字符串方法,详细说明可以看MDN的说明. 如果正则表达式匹配成功的话,match方法会返回一个数组,而数组里的数据有两种形式,对应着匹配方式:全局匹配与非全局匹配. 1. 全局匹 ...
- Android学习笔记之Bitmap位图的旋转
位图的旋转也可以借助Matrix或者Canvas来实现. 通过postRotate方法设置旋转角度,然后用createBitmap方法创建一个经过旋转处理的Bitmap对象,最后用drawBitmap ...
- ie为什么那么垃圾(不是ie垃圾,是ie用的人太多了,很多在用低版本)
ie为什么那么垃圾(不是ie垃圾,是ie用的人太多了,很多在用低版本) 一.总结 1.我们觉得ie差的原因:我们拿老的ie和最新的其它浏览器做比较了,两者相差了很多年.比较微软几十年才发布了11个ie ...
- Chome 浏览器,您的连接不是私密连接
网上搜索了半天,最后才发现是去广告插件/软件的原因, 关掉广告插件/者软件,或者重新导入证书, 就可以了.
- JSP学习 —— 开篇:JSP,servlet容器,Tomcat,servlet容器之间的关系
JSP(JAVA SERVER PAGE)的缩写,其本身就是servlet的简化,是一种动态网页标准,其特点是在HTML代码中嵌入JAVA代码,JSP标签或用户标签来生成网页.至于它为什么会出现,主要 ...
- StringBuilder和String的区别
使用 StringBuilder 语言 C# String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为 ...
- 深入理解线程本地变量ThreadLocal
ThreadLocal理解: 假设在多线程并发环境中.一个可变对象涉及到共享与竞争,那么该可变对象就一定会涉及到线程间同步操作,这是多线程并发问题. 否则该可变对象将作为线程私有对象,可通过Threa ...