通过适配器去取得联系人名单 并现实电话号码
适配器
package zhang.example;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
public class ContactsAdapter extends CursorAdapter{
private ContentResolver mContent;
public ContactsAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
mContent = context.getContentResolver();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
//取得通讯录人员的名字
((TextView)view).setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view=(TextView)inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent,false);
view.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
return view;
}
public String converTOString(Cursor cursor){
return cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
}
public Cursor runQCursorOnBackgrpundThread(CharSequence constraint){
if(getFilterQueryProvider()!=null){
return getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer=null;
String[] args=null;
if(constraint!=null){
buffer=new StringBuilder();
buffer.append("UPPER(");
buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
buffer.append(")GLOB?");
args=new String[]{
constraint.toString().toUpperCase()+"*"
};
}
return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, contentResolver.PEOPLE_PROJECTION, buffer==null?null:buffer.toString(), args, "");
}
}
主要的activity
package zhang.example;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
public class ContactsAdapter extends CursorAdapter{
private ContentResolver mContent;
public ContactsAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
mContent = context.getContentResolver();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
//取得通讯录人员的名字
((TextView)view).setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view=(TextView)inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent,false);
view.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
return view;
}
public String converTOString(Cursor cursor){
return cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
}
public Cursor runQCursorOnBackgrpundThread(CharSequence constraint){
if(getFilterQueryProvider()!=null){
return getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer=null;
String[] args=null;
if(constraint!=null){
buffer=new StringBuilder();
buffer.append("UPPER(");
buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
buffer.append(")GLOB?");
args=new String[]{
constraint.toString().toUpperCase()+"*"
};
}
return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, contentResolver.PEOPLE_PROJECTION, buffer==null?null:buffer.toString(), args, "");
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myTextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
需要配置权限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>