天天看點

Android中擷取聯系人資訊(二) 使用AsyncQueryHandler

    最近有看到了一種擷取聯系人資訊的寫法,自己試了一下感覺不錯,在此記錄一下

    學習思路來源http://blog.csdn.net/wwj_748/article/details/19965913

    在這篇中和上一篇寫法的差別有幾點:

    1、使用了AsyncQueryHandler來代替handler

    2、一次周遊查出了所有聯系人,而是不嵌套兩層周遊。是以查詢的速度比之前快了很多,于是我把進度條也去掉了

代碼如下:

package com.example.contactlist;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Loading extends ListActivity {
	Context myContext = null;
	MyListAdapter myListAdapter = null;
	ViewHolder viewHolder = null;

    private AsyncQueryHandler asyncQueryHandler; // 異步查詢資料庫類對象    

	// 用于存儲聯系人名稱
	List<String> myContactName = new ArrayList<String>();
	// 用于存儲聯系人電話
	List<String> myContactNumber = new ArrayList<String>();
	// 用于存儲聯系人總數
	int myContactAmount = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        asyncQueryHandler = new MyAsyncQueryHandler(getContentResolver());   

		getContactList();
	}


	private void getContactList() {

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // 聯系人Uri;    
        // 查詢的字段    
        String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,   
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,   
                ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key",   
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,   
                ContactsContract.CommonDataKinds.Phone.PHOTO_ID,   
                ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY };   
        // 按照sort_key升序查詢    
        asyncQueryHandler.startQuery(0, null, uri, projection, null, null,   
                "sort_key COLLATE LOCALIZED asc");   
	
	}
	
	
    private class MyAsyncQueryHandler extends AsyncQueryHandler {   
    	   
        public MyAsyncQueryHandler(ContentResolver cr) {   
            super(cr);   
        }   
   
          
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {   
            if (cursor != null && cursor.getCount() > 0) {   
             
                cursor.moveToFirst(); // 遊标移動到第一項    
                for (int i = 0; i < cursor.getCount(); i++) {   
                    cursor.moveToPosition(i);   
                    String name = cursor.getString(1);   
                    String number = cursor.getString(2);   
         
                    myContactName.add(name);
                    myContactNumber.add(number);
                    
            }  
                myListAdapter = new MyListAdapter(Loading.this);
				setListAdapter(myListAdapter);
   
            super.onQueryComplete(token, cookie, cursor);   
        }   
   
    }   
    }
	

	class MyListAdapter extends BaseAdapter {

		public MyListAdapter(Context context) {
			myContext = context;
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return myContactName.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			if (convertView == null) {
				viewHolder = new ViewHolder();

				convertView = LayoutInflater.from(myContext).inflate(
						R.layout.list, null);
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.name);
				viewHolder.number = (TextView) convertView
						.findViewById(R.id.number);

				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.name.setText(myContactName.get(position));
			viewHolder.number.setText(myContactNumber.get(position));

			return convertView;
		}
	}

	private static class ViewHolder {
		TextView name;
		TextView number;

	}
}