异步的查询操作帮助类 AsyncQueryHandler 详解

AsyncQueryHandler

extends Handler

java.lang.Object

   ↳

android.os.Handler

 

   ↳

android.content.AsyncQueryHandler

 

AsyncQueryHandler:异步的查询操作帮助类,使用它可以更简单的对ContentResolver进行操作。

构造方法

AsyncQueryHandler(ContentResolver cr)

 

公共方法

final void

cancelOperation(int token)

动作还没有开始前,取消操作

void

handleMessage(Message msg)

子类必须实现这个来接收消息。

final void

startDelete(int token, Object cookie, Uri uri, String selection, String[] selectionArgs)

一个异步删除的方法。

final void

startInsert(int token, Object cookie, Uri uri, ContentValues initialValues)

执行一个异步的插入.

void

startQuery(int token, Object cookie, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy)

执行一个异步的查询

final void

startUpdate(int token, Object cookie, Uri uri, ContentValues values, String selection, String[] selectionArgs)

执行一个异步的更新

 

受保护方法

Handler

createHandler(Looper looper)

void

onDeleteComplete(int token, Object cookie, int result)

异步删除完成时调用。

void

onInsertComplete(int token, Object cookie, Uri uri)

异步插入完成时调用。

void

onQueryComplete(int token, Object cookie, Cursor cursor)

异步查询完成时调用。

void

onUpdateComplete(int token, Object cookie, int result)

异步更新完成时调用。

 

public final void startDelete (int token, Object cookie, Uri uri, String selection, String[] selectionArgs)
参数

token

操作的辨识标记

cookie

一个对象被传递到 onDeleteComplete(int, Object, int)中去

uri

需要执行删除操作的URI

selection

条件参数

 

public final void startInsert (int token, Object cookie, Uri uri, ContentValues initialValues)
参数

token

在 onInsertComplete(int, Object, Uri)中需要的辨识Token

cookie

传入一个对象到onInsertComplete(int, Object, Uri)中去

uri

需要执行插入操作的URI

initialValues

需要插入的值

 

 

public void startQuery (int token, Object cookie, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy)
参数

token

在onQueryComplete中用作操作标识

cookie

传入 onQueryComplete(int, Object, Cursor)变量参数

uri

The URI, using the content:// scheme, for the content to retrieve.

projection

需要查询的列

selection

查询的条件语句

selectionArgs

查询的条件参数

orderBy

结果排序操作与 SQL 中的ORDER BY 一致

 

 

public final void startUpdate (int token, Object cookie, Uri uri, ContentValues values, String selection, String[] selectionArgs)
参数

token

传入 onUpdateComplete(int, Object, int) 中的辨识符

cookie

传入 onUpdateComplete(int, Object, int)中的参数

uri

执行更新操作的URI

values

需要更新的值

 


一个简单的 查询demo

 private static final int QUERY_CONTACT = 1;
    private static final int QUERY_GROUP = 2;
    private Uri contact_uri = Phone.CONTENT_URI; // 联系人的Uri
    String[] projection =
    {
        Phone._ID,
        Phone.DISPLAY_NAME,
        Phone.DATA1,
        Phone.SORT_KEY_PRIMARY,
        Phone.CONTACT_ID,
        Phone.PHOTO_ID,
        Phone.LOOKUP_KEY
}; // 查询的列


  /**
     * 数据库异步查询类AsyncQueryHandler
     * 
     * @author administrator
     */
    private class ContactQueryHandler extends AsyncQueryHandler
    {

        public ContactQueryHandler(ContentResolver cr)
        {
            super(cr);
        }

        /**
         * 查询结束的回调函数
         */
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor)
        {
            switch (token)
            {
                case QUERY_CONTACT:
                    getContact(cursor);
                    break;
                case QUERY_GROUP:
                    break;
                default:
                    break;
            }
        }

    }

private void getContact(Cursor cursor)
    {
        List<ContactBean> contacts = new ArrayList<ContactBean>();
        if (cursor != null && cursor.getCount() > 0)
        {

            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++)
            {
                ContactBean contactBean;
                cursor.moveToPosition(i);
                String name = cursor.getString(1);
                String number = cursor.getString(2);
                String sortKey = cursor.getString(3);
                int contactId = cursor.getInt(4);
                Long photoId = cursor.getLong(5);
                String lookUpKey = cursor.getString(6);

                if (contactSparse.get(contactId) != null)
                {
                    contactBean = contactSparse.get(contactId);
                }
                else
                {
                    contactBean = new ContactBean();
                    contactBean.displayName = name;
                    contactBean.phoneNum = number;
                    contactBean.sortKey = sortKey;
                    contactBean.contactId = contactId;
                    contactBean.photoId = photoId;
                    contactBean.lookUpKey = lookUpKey;

                    contactSparse.put(contactId, contactBean);
                }
                contacts.add(contactBean);
            }
            if (m_getContactListener != null)
                m_getContactListener.onGetContact(contacts);
        }

执行,查询query就能够得到了


asyncQuery.startQuery(QUERY_CONTACT, null, contact_uri, projection, null, null,
                "sort_key COLLATE LOCALIZED asc"); // 按照sort_key升序查询


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值