【问题标题】:Android - Find a contact by display nameAndroid - 按显示名称查找联系人
【发布时间】:2012-03-08 21:34:49
【问题描述】:

我正在尝试按显示名称查找联系人。目标是打开此联系人并向其添加更多数据(特别是更多电话号码),但我什至很难找到我想要更新的联系人。

这是我正在使用的代码:

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

我确实有一个名为“John Johnson”的联系人,但该方法总是返回“未找到”。我还尝试搜索只有一个名字的联系人,所以这没有区别。

我怀疑 uri、selection 或 selection 参数有问题,因为我在网上找不到任何搜索具有给定显示名称的联系人的示例,而且显示名称似乎是一种特殊的信息,不同例如电话号码。

我有什么办法可以找到约翰·约翰逊吗?


更新:我发现了如何通过显示名称查找联系人:

        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";

此代码返回显示名称为“John Johnson”的第一个联系人的联系人 ID。在我的原始代码中,我的查询中有错误的 uri 和错误的选择。

【问题讨论】:

    标签: android android-contentresolver


    【解决方案1】:

    我认为问题可能是由您设置的投影引起的。投影用于告诉android您要查询哪一列数据,然后您只提供id列,因此显示名称不会返回。尝试移除投影,看看它是否有效。

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

    【讨论】:

    • 感谢您的回复,但投影无效并没有帮助。当我搜索电话号码时,相同的代码有效,因此即使我在另一列中搜索匹配项,也可以将 PhoneLookup._ID 作为投影。如果我理解正确,投影是您希望从查询中返回的数据,而不是您正在搜索的数据。因此,如果您将投影设置为 null,您只是要求从您从查询中获得的匹配联系人中获取所有数据。
    【解决方案2】:

    更改您的查询 URI。

    您使用的 URI 仅用于过滤电话号码:

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI;
    

    您需要使用可以访问display_name 列的 URI,如下所示:

    Uri uri = ContactsContract.Data.CONTENT_URI;
    

    在Android SDK Documentation 上有一个关于使用什么 URI 以及何时使用它们的详细分类:

    • 如果您需要读取单个联系人,请考虑使用 CONTENT_LOOKUP_URI 而不是 CONTENT_URI。

    • 如果您需要通过电话号码查找联系人,请使用为此目的优化的 PhoneLookup.CONTENT_FILTER_URI。

    • 如果您需要按部分姓名查找联系人,例如要生成按您输入的过滤器建议,请使用 CONTENT_FILTER_URI URI。

    • 如果您需要通过电子邮件地址、昵称等数据元素查找联系人,请使用针对 ContactsContract.Data 表的查询。结果将包含联系人 ID、姓名等。

    【讨论】:

      【解决方案3】:
                 //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 ...
      

      【讨论】:

        【解决方案4】:

        下面的代码应该可以解决问题

          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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-03-02
          • 1970-01-01
          • 1970-01-01
          • 2016-01-28
          • 2011-05-19
          • 2022-10-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多