zoukankan      html  css  js  c++  java
  • AsyncTask和AsyncQueryHandler之比较

    定义AsyncTask子类

    private class LoadContactsTask extends AsyncTask<Void, Void, List<TxrjContact>> {

        /* (non-Javadoc)
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected List<TxrjContact> doInBackground(Void... params) {
            List<TxrjContact> contacts = ContactDataManager.getContacts(mContext);
            return contacts;
        }
        /* (non-Javadoc)
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = ProgressDialog.show(mContext,
                    null, "loading contacts, please wait a moment");
        }
        /* (non-Javadoc)
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(List<TxrjContact> result) {
            super.onPostExecute(result);
            mContacts = result;
            mListAdapter = new ContactListAdapter(mContext, mContacts, mQuickAlphaBar);
            mListView.setAdapter(mListAdapter);
            mProgressDialog.dismiss();
        }
    }

    在onCreate方法中使用AsyncTask

    new LoadContactsTask().execute();

     

    定义AsyncQueryHandler的子类

    private class AsyncQueryContacts extends AsyncQueryHandler {

        public AsyncQueryContacts(ContentResolver cr) {
            super(cr);
        }
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor c) {
            List<TxrjContact> contacts = new ArrayList<TxrjContact>();
            HashMap<Integer, TxrjContact> contactMap = new HashMap<Integer, TxrjContact>();
            if(c != null) {
                while(c.moveToNext()) {
                    int contactId = c.getInt(c.getColumnIndex(Data.CONTACT_ID));
                    TxrjContact contact = null;
                    if(contactMap.containsKey(contactId)) {
                        contact = contactMap.get(contactId);
                    } else {
                        contact = new TxrjContact();
                        contact.setContactId(contactId);
                        contact.setName(c.getString(c.getColumnIndex(Phone.DISPLAY_NAME)));
                        contact.setSortKey(c.getString(c.getColumnIndex("sort_key")));
                        contactMap.put(contactId, contact);
                        contacts.add(contact);
                    }
                    TxrjPhone phone = new TxrjPhone();
                    phone.setId(c.getInt(c.getColumnIndex(Data._ID)));
                    phone.setRawContactId(c.getInt(c.getColumnIndex(Data.RAW_CONTACT_ID)));
                    phone.setContactId(c.getInt(c.getColumnIndex(Data.CONTACT_ID)));
                    phone.setNumber(c.getString(c.getColumnIndex(Phone.NUMBER)));
                    phone.setType(c.getString(c.getColumnIndex(Phone.TYPE)));
                    phone.setLabel(c.getString(c.getColumnIndex(Phone.LABEL)));
                    contact.getPhoneList().add(phone);
                }
                c.close();
            }
            mContacts = contacts;
            mListAdapter = new ContactListAdapter(mContext, mContacts, mQuickAlphaBar);
            mListView.setAdapter(mListAdapter);
            mProgressDialog.dismiss();
            super.onQueryComplete(token, cookie, c);
        }
    }

    在onCreate方法中使用AsyncQueryHandler

    private AsyncQueryContacts asyncQuery;

    asyncQuery = new AsyncQueryContacts(getContentResolver());
    mProgressDialog = ProgressDialog.show(mContext,
            null, "loading contacts, please wait a moment");
    asyncQuery.startQuery(0, null, Data.CONTENT_URI,
        new String[] {Data._ID, Data.CONTACT_ID, Data.RAW_CONTACT_ID,
            Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, Phone.LABEL, "sort_key"},
        Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
        null, "sort_key COLLATE LOCALIZED asc");

  • 相关阅读:
    Spring视频学习笔记(一)
    枚举(enum)与类的静态成员(static)
    Java的继承模式
    HTML锁定Table中某一列
    Winform 中的KeyDown
    我的WebService入门
    获取键盘或鼠标多久没有对屏幕进行操作了
    从DB输出值到DataTable时,字段值为NULL时报错相关信息;
    Data层相关问题 & JS循环取值
    字符串String的trim()方法
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/3181234.html
Copyright © 2011-2022 走看看