2012-03-08 63 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

相关问题