10

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

这是我正在使用的代码:

    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 和错误的选择。

4

4 回答 4

1

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

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

于 2012-03-09T02:37:06.623 回答
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 ...
于 2014-08-17T17:20:27.817 回答
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;
    }

参考:检索联系人列表文章

于 2017-07-19T03:47:51.347 回答
0

更改您的查询 URI。

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

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI;

您需要使用有权访问该display_name列的 URI,如下所示:

Uri uri = ContactsContract.Data.CONTENT_URI;

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

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

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

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

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

于 2018-05-10T03:43:49.487 回答