---
title: "ContactsContract读取联系人的异步方法"
author: "zymic"
date: "2010-08-15T11:36:46Z"
tags: []
categories: ["移动开发"]
slug: "contactscontract"
draft: false
lastmod: "2010-08-15T11:36:46Z"
id: 737681
url: "https://zymic.iteye.com/blog/737681"
---

用AsyncTask异步的方法读取联系人列表方法：

1）在主线程的Activity下加入代码 new MyAsyncTask(this,allcontacts\_cursor).execute(lv);MyAsyncTask继承自AsyncTask类，lv是ListView的对象，是传入的参数；this是当前ListActivity的Context对象；allcontacts\_cursor是在当前主线程中得到的所有联系人的Cursor对象；

2)MyAsyncTask类定义如下：public class MyAsyncTask extends AsyncTask&lt;ListView, Void, BaseAdapter&gt;

ListView 是传入的参数，BaseAdapter是AsyncTask类中方法BaseAdapter doInBackground(ListView... params)的返回的结果,doInBackground方法的传入参数既是new MyAsyncTask(this,allcontacts\_cursor).execute(lv)方法中的lv;

在方法doInBackground中主要是实例化一个adapter，并将其返回。其返回值作为方法onPostExecute(BaseAdapter result)；的参数传入；

在方法onPostExecute(BaseAdapter result)中将输入参数赋值给MyAsyncTask对象的输入参数；

3）LayoutInflater的调用；

a)LayoutInflater inflater = Activity.getLayoutInflater()

b)LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT\_INFLATER\_SERVICE);