android 通讯录的使用 通过搜索名字自己出现电话号码

受限增加权限

  <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, "");
 }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值