Android异步查询框架AsyncQueryHandler使用简介

1.首先是写一个查询的方法

//查询数据
    private void startQuery() {
        Uri uri=Sms.CONTENT_URI;
        String selection=" thread_id=?";
        String[] selectionArgs=new String[]{thread_id};
        //查询
        //projection 查询的字段
        queryHandler.startQuery(0, null, uri, SMS_PROJECTION, selection, selectionArgs, " date DESC");
    }

 

2.把需要查询的字段给抽取出来。

/**
     * 将查询所用的字段处理出来
     */
    private static final String[] SMS_PROJECTION=new String[]{
        "_id",
        "address",
        "date",
        "type",
        "body",
    };
    /**
     * 当前字段所对应的位置
     */
    private static final int ID_COLUMN_INDEX = 0;
    private static final int ADDRESS_COLUMN_INDEX = 1;
    private static final int DATE_COLUMN_INDEX = 2;
    private static final int TYPE_COLUMN_INDEX = 3;
    private static final int BODY_COLUMN_INDEX = 4;

 

3.然后继承异步查询框架

private class QueryHandler extends AsyncQueryHandler{

        public QueryHandler(ContentResolver cr) {
            super(cr);
        
        }
        
        //查询执行完成后悔执行这个方法
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            // TODO Auto-generated method stub
            super.onQueryComplete(token, cookie, cursor);
            //界面更新
            adapter.changeCursor(cursor);
        }
    }
posted @ 2012-12-28 18:43  Andye  阅读(2972)  评论(0编辑  收藏  举报