还记得这篇文章吗?​​Android ListView 最基本的用法,使用SimpleCursorAdapter 。 附例子。​​我在使用的时候,载入大量数据时,开始会很卡,滑动倒是不会卡,就是因为开始载入是用一条sql语句查询所有数据。Android中遇到这样的问题一般用线程来执行。看了下Android 4.0 Contacts的载入方式,它非常流畅。发现它用了CursorLoader ,这里用它来做一个小小的例子。左边的是刚开始的效果,你也可以改成一个dialog罩住不让用户用。右边的是载入完成的样子,设计到联系人隐私,画了下。

CursorLoader 还是蛮好用的不过它Added in API level 11,Android 3.0才能用。Android2.3即以下都不能用,比较遗憾。

哦,对了,下面这应用别忘记加上读取联系人权限。


Android ListView 正在加载 异步载入数据 CursorLoader 例子_数据

                

Android ListView 正在加载 异步载入数据 CursorLoader 例子_数据_02


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, //0
ContactsContract.Contacts.DISPLAY_NAME//1
};

private Context mContext;
public MyCursorLoader(Context context) {
super(context);
mContext = context;
}
/**
* 查询数据等操作放在这里执行
*/
@Override
protected 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>