2012-03-08 62 views
8

我試圖通過顯示名稱查找聯繫人。目標是打開此聯繫人並向其中添加更多數據(特別是更多電話號碼),但我很難找到要更新的聯繫人。Android - 通過顯示名稱查找聯繫人

這是我使用的代碼:

public static String findContact(Context context) { 

    ContentResolver contentResolver = context.getContentResolver(); 
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI; 
    String[] projection = new String[] { PhoneLookup._ID }; 
    String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ?"; 
    String[] selectionArguments = { "John Johnson" }; 
    Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null); 

    if (cursor != null) { 
     while (cursor.moveToNext()) { 
      return cursor.getString(0); 
     } 
    } 
    return "John Johnson not found"; 
} 

我有一個名爲「約翰·約翰遜」接觸,但該方法總是返回「未找到」。我也嘗試用一個名字搜索聯繫人,所以這沒有什麼區別。

我懷疑它的uri,選擇或選擇參數有問題,因爲我找不到任何搜索具有給定顯示名稱的聯繫人的示例,並且它似乎顯示名稱是一種特殊類型的信息,不同於例如電話號碼。

任何想法如何找到約翰遜約翰遜?


更新:我發現瞭如何找到顯示名稱聯繫人:

 ContentResolver contentResolver = context.getContentResolver(); 
    Uri uri = Data.CONTENT_URI; 
    String[] projection = new String[] { PhoneLookup._ID }; 
    String selection = StructuredName.DISPLAY_NAME + " = ?"; 
    String[] selectionArguments = { "John Johnson" }; 
    Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null); 

    if (cursor != null) { 
     while (cursor.moveToNext()) { 
      return cursor.getString(0); 
     } 
    } 
    return "John Johnson not found"; 

此代碼返回與顯示名稱爲「約翰·約翰遜的」第一次接觸的接觸ID。在我的原始代碼中,我的查詢中有錯誤的uri和錯誤的選擇。

回答

1

我認爲這個問題可能是由您設置的投影引起的。投影用於告訴android你想要查詢的數據列,然後你只給id列,以便顯示名稱不會返回。嘗試刪除投影以查看它是否有效。

-- Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);
++ Cursor cursor = contentResolver.query(uri, null, selection, selectionArguments, null);

+1

感謝回答,但它並沒有幫助有空投影。當我搜索電話號碼時,相同的代碼有效,因此即使我在另一列中搜索匹配,也可以將PhoneLookup._ID用作投影。 如果我正確地理解了它,那麼投影就是你想從查詢中得到的數據,而不是你要搜索的數據。因此,如果您將投影設置爲null,則只需要從您的查詢中獲取匹配聯繫人的所有數據即可。 – 2012-03-10 18:58:48

0
  //method for gaining id 
//this method get a name and make fetch it's id and then send the id to other method //named "showinformation" and that method print information of that contact 
     public void id_return(String name) { 
       String id_name=null; 
       Uri resultUri = ContactsContract.Contacts.CONTENT_URI; 
       Cursor cont = getContentResolver().query(resultUri, null, null, null, null); 
       String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " = ?" ; 
       String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,name}; 
       Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
       while (nameCur.moveToNext()) { 
       id_name = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID));} 
       nameCur.close(); 
       cont.close(); 
       nameCur.close(); 
//for calling of following method 
       showinformation(id_name); 
      } 

      //method for showing information like name ,phone, email and other thing you want 
      public void showinformation(String id) { 
       String name=null; 
       String phone=null; 
       String email=null; 
       Uri resultUri = ContactsContract.Contacts.CONTENT_URI; 
       Cursor cont = getContentResolver().query(resultUri, null, null, null, null); 
       String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID+ " = ?" ; 

       String[] whereNameParams1 = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,id}; 
       Cursor nameCur1 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams1, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
       while (nameCur1.moveToNext()) { 
       name = nameCur1.getString(nameCur1.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));} 
       nameCur1.close(); 
       cont.close(); 
       nameCur1.close(); 


       String[] whereNameParams2 = new String[] { ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,id}; 
       Cursor nameCur2 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams2, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
       while (nameCur2.moveToNext()) { 
       phone = nameCur2.getString(nameCur2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));} 
       nameCur2.close(); 
       cont.close(); 
       nameCur2.close(); 


       String[] whereNameParams3 = new String[] { ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,id}; 
       Cursor nameCur3 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams3, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
       while (nameCur3.moveToNext()) { 
       email = nameCur3.getString(nameCur3.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));} 
       nameCur3.close(); 
       cont.close(); 
       nameCur3.close(); 

       String[] whereNameParams4 = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,id}; 
       Cursor nameCur4 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams4, ContactsContract.CommonDataKinds.StructuredPostal.DATA); 
       while (nameCur4.moveToNext()) { 
       phone = nameCur4.getString(nameCur4.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA));} 
       nameCur4.close(); 
       cont.close(); 
       nameCur4.close(); 
    //showing result 
      txadd.setText("Name= "+ name+"\nPhone= "+phone+"\nEmail= "+email); 


      } 

//thank all persons in this site because of many help of me to learn and correction my warn and errors this is only a gift for all of you and ... 
0

下面的代碼應該做的伎倆

if (displayName != null) { 
     Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(displayName)); 
     String[] displayNameProjection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME }; 
     Cursor cur = context.getContentResolver().query(lookupUri, displayNameProjection, null, null, null); 
     try { 
      if (cur.moveToFirst()) { 
       return true; 
      } 
     } finally { 
      if (cur != null) 
       cur.close(); 
     } 
     return false; 
    } else { 
     return false; 
    } 

參考:Retrieving a List of Contacts Article

相關問題