Android ListView 正在加载 异步载入数据 CursorLoader 例子
来源:互联网 发布:pc蛋蛋预测php源码 编辑:程序博客网 时间:2024/06/05 11:56
还记得这篇文章吗?Android ListView 最基本的用法,使用SimpleCursorAdapter 。 附例子。我在使用的时候,载入大量数据时,开始会很卡,滑动倒是不会卡,就是因为开始载入是用一条sql语句查询所有数据。Android中遇到这样的问题一般用线程来执行。看了下Android 4.0 Contacts的载入方式,它非常流畅。发现它用了CursorLoader ,这里用它来做一个小小的例子。左边的是刚开始的效果,你也可以改成一个dialog罩住不让用户用。右边的是载入完成的样子,设计到联系人隐私,画了下。
CursorLoader 还是蛮好用的不过它Added in API level 11,Android 3.0才能用。Android2.3即以下都不能用,比较遗憾。
哦,对了,下面这应用别忘记加上读取联系人权限。
MainActivity.java
package com.waitingfy.android;import android.app.ListActivity;import android.app.LoaderManager.LoaderCallbacks;import android.content.Loader;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContract;import android.widget.SimpleCursorAdapter;import android.widget.TextView;public class MainActivity extends ListActivity implements LoaderCallbacks<Cursor> {//1.首先要实现这接口private TextView mNoticeMessage;private SimpleCursorAdapter mAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mNoticeMessage = (TextView)findViewById(R.id.notice_message); getLoaderManager().initLoader(1, null, this);//2.要初始化Loader }public Loader<Cursor> onCreateLoader(int id, Bundle args) {return new MyCursorLoader(getApplicationContext());//3.去数据库读取数据等要消耗大量时间的操作放在 //自定义 CursorLoader 的 onLoadInBackground} /** * 4.自定义 CursorLoader 的 onLoadInBackground * 会返回一个Cursor,这里给SimpleCursorAdapter用 * 来填充数据。 */public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {mAdapter = new SimpleCursorAdapter(MainActivity.this,R.layout.contacts_list_item, cursor, new String[]{ContactsContract.Contacts.DISPLAY_NAME},new int[]{ android.R.id.text1});setListAdapter(mAdapter);mNoticeMessage.setText(getResources().getString(R.string.count_string,mAdapter.getCount()));}public void onLoaderReset(Loader<Cursor> arg0) {// TODO Auto-generated method stub}}
MyCursorLoader.java
package com.waitingfy.android;import android.content.Context;import android.content.CursorLoader;import android.database.Cursor;import android.provider.ContactsContract;public class MyCursorLoader extends CursorLoader{String[] mContactProjection={ContactsContract.Contacts._ID, //0ContactsContract.Contacts.DISPLAY_NAME//1};private Context mContext;public MyCursorLoader(Context context) {super(context);mContext = context;}/** * 查询数据等操作放在这里执行 */@Overrideprotected Cursor onLoadInBackground() {Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, mContactProjection, null,null, null);return cursor;}}
layout/contacts_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="64dip" android:orientation="horizontal" android:gravity="center_vertical" android:paddingLeft="8dip"><TextView android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:gravity="center_vertical" android:singleLine="true" android:fadingEdge="horizontal" android:fadingEdgeLength="3dip" android:ellipsize="marquee" android:textColor="#FFFFFF" android:layout_weight="1"/></LinearLayout>
layout/main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/notice_message" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/on_loading" /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="match_parent"> </ListView></LinearLayout>
values/strings.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">CursorLoadSample</string> <string name="on_loading">正在加载</string> <string name="count_string">您有%s记录</string></resources>
http://www.waitingfy.com/?p=522
CursorLoadSample例子下载
- Android ListView 正在加载 异步载入数据 CursorLoader 例子
- Android之cursorLoader进行数据异步加载
- 使用CursorLoader异步加载数据
- 使用CursorLoader异步加载数据
- CursorLoader异步加载
- CursorLoader为ListFragment中ListView加载数据的流程
- Android中ListView异步加载数据
- Android中ListView异步加载数据
- Android中ListView异步加载数据
- Android中ListView异步加载数据
- Android中ListView异步加载数据
- Android:ListView数据异步加载、Handler、AsyncTask
- Android中ListView异步加载数据
- Android中ListView异步加载数据
- Android中ListView异步加载数据
- CursorLoader与ContentProvider结合,实现异步加载
- CursorLoader异步加载信鸽推送消息
- ListView异步加载网络数据
- Eclipse使用Git代替CVS与SVN实现版本管理
- CC、CQ已可用了,你懂的
- zoj 1108 FatMouse's Speed
- Visual Studio 2010 编译 MongoDB C++ Client Driver(看这里,终于解决了编译问题,谢谢大神的分享)
- web.xml配置文件概述
- Android ListView 正在加载 异步载入数据 CursorLoader 例子
- JNI学习系列(二)
- poj2630Phone List(静态Trie tree)
- Android无线调试——抛开USB数据线
- POJ 1004
- 珠海高新区唐家湾镇计生办事程序
- dhtmlx tree在IE9下不显示的问题
- USB设备名字
- HDU3001——Travelling(状态压缩DP)