受限增加权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
MainActivity如下
private AutoCompleteTextView myautoTextview;
private Cursor contactCursor;
private TextView myTextView;
public static final String[] PHONE_PROJECT = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME };
private ContactsAdapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myautoTextview = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
myTextView = (TextView) findViewById(R.id.textView1);
ContentResolver content = getContentResolver();
// 获得通讯录的cursor
contactCursor = content.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PHONE_PROJECT, null, null, "");
myadapter = new ContactsAdapter(this, contactCursor);
myautoTextview.setAdapter(myadapter);
myautoTextview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Cursor c = myadapter.getCursor();
// 移到点击位置
c.moveToPosition(arg2);
String number = c.getString(c
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
// 当找不到电话时候显示无输入电话
number = number == null ? "无输入电话" : number;
myTextView.setText(c.getString(c
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))
+ "的电话是" + number);
}
});
}
ContactsAdapter如下:
public class ContactsAdapter extends CursorAdapter {
private ContentResolver mContent;
public ContactsAdapter(Context context, Cursor c) {
super(context, c);
mContent = context.getContentResolver();
}
@Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
((TextView) arg0).setText(arg2.getString(arg2
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
}
@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
final LayoutInflater infater = LayoutInflater.from(arg0);
final TextView tv = (TextView) infater.inflate(
android.R.layout.simple_dropdown_item_1line, arg2, false);
tv.setText(arg1.getString(arg1
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
return tv;
}
@Override
public CharSequence convertToString(Cursor cursor) {
return cursor.getString(cursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
}
@Override
public Cursor runQueryOnBackgroundThread(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,
MainActivity.PHONE_PROJECT,
buffer == null ? null : buffer.toString(), args, "");
}
}