【问题标题】:Custom Search for contacts in android在android中自定义搜索联系人
【发布时间】:2015-01-10 11:21:03
【问题描述】:

我想在 android 中自定义搜索联系人。

例如,我想要每个联系人号码以 555 结尾的联系人,我该怎么做?

【问题讨论】:

标签: android sqlite android-contentprovider android-contacts


【解决方案1】:

获取所有联系人并使用代码手动搜索。

ContentResolver cr = getContentResolver(); 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
String phone = null;
if (cur.getCount() > 0) { 
  while (cur.moveToNext()) { 
    String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID)); 
    if (Integer .parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
    { 
      Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); 
      while (pCur.moveToNext()) { 
        phone = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        if(phone.endsWith( "555")
             // store the number and id of the contact
      } 
      pCur.close(); 
    } 
    /*** Store id and phone in another variable(eg ArrayList,etc) so that it does not get lost in another iteration of while loop***/
  }
}

Android 联系人也使用空格、- 和 () 进行格式化。在检查之前删除它们可能会更好。使用

phone.replace("-", "").replace("(", "").replace(")", "").replace(".", "").replace(" ", "");

另见here

【讨论】:

  • 感谢 mohammad ali,我看到了你的答案,但我正在寻找更快的方法,我会找到的。
  • 也在这里查看我的答案:stackoverflow.com/questions/26859338/… 它使用 PhoneLookup.CONTENT_FILTER_URI 它比上面的代码更快...
  • 你找到更快的方法了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 2015-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多