Custom Search for contacts in android

前提是你 提交于 2019-12-02 20:02:41

问题


I want to custom search in contacts in android.

For example, i want contacts that each contact number end with 555 so how can i do that?


回答1:


Get all contacts and search manually using code.

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***/
  }
}

Also Android contacts are formatted with spaces,- and (). it may be better to remove them before checking. Using

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

Also see here.



来源:https://stackoverflow.com/questions/26906756/custom-search-for-contacts-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!