获取联系人的名字和姓氏而不是单个显示名称?

发布于 2024-12-20 03:11:09 字数 2820 浏览 2 评论 0原文

我目前正在与 Android 联系人内容提供商合作,目前可以使用以下代码访问联系人完整显示名称,不会出现问题:

String[] PROJECTION = new String[] {
         ContactsContract.Contacts._ID,
         ContactsContract.Contacts.DISPLAY_NAME,
         ContactsContract.Contacts.HAS_PHONE_NUMBER,
        };
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

String SELECTION = "LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ")" 
    + " LIKE '" + constraint + "%' " + "and " + 
        ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";

Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI, 
                PROJECTION, SELECTION, null, sortOrder);

但是,我希望能够分别获取联系人的名字和姓氏,我尝试使用 StructuredName试图得到这个,但我似乎无法让它工作。

任何人都可以为我指明如何正确使用 StructuredName 将名称拆分为 First 和 Last 的正确方向吗?

更新:

按照hovanessyan的建议,我尝试了以下操作:

    String[] PROJECTION = new String[] {
            ContactsContract.Data._ID,
            ContactsContract.Data.DISPLAY_NAME,
            ContactsContract.Data.HAS_PHONE_NUMBER,
    };

    String SELECTION = ContactsContract.CommonDataKinds.StructuredName.IN_VISIBLE_GROUP + " = '1'";

    String sortOrder = ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, sortOrder);

    int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
    int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);

    while (cursor.moveToNext()) {
        String given = cursor.getString(indexGivenName);
        String family = cursor.getString(indexFamilyName);
        String display = cursor.getString(indexDisplayName);
        Log.e("XXX", "Name: | " + given + " | " + family + " | " + display);
    } 

但是,使用投影会导致崩溃,如下所示:

12-08 16:52:01.575: E/AndroidRuntime(7912): Caused by: java.lang.IllegalArgumentException: Invalid column has_phone_number

如果删除投影,我会打印出所有结果,但其中很多包含NULL。

例如:

12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null

那么任何人都可以看到我做错了什么,因为我的投影不起作用吗?

进一步更新:

我已经解决了投影的问题,但现在我遇到一个问题,数据内容提供程序向我提供所有空数据并导致我的代码中出现空指针异常。

例如,ContactsContract.Contacts 中的游标计数返回 115,但使用 DATA 表使用相同的参数返回 464,这在我的应用程序中引起了巨大问题。

任何人都知道为什么会这样?

I am currently working with the Android Contacts content provider and currently can access a contacts full display name without issue using the following code:

String[] PROJECTION = new String[] {
         ContactsContract.Contacts._ID,
         ContactsContract.Contacts.DISPLAY_NAME,
         ContactsContract.Contacts.HAS_PHONE_NUMBER,
        };
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

String SELECTION = "LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ")" 
    + " LIKE '" + constraint + "%' " + "and " + 
        ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";

Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI, 
                PROJECTION, SELECTION, null, sortOrder);

However I want to be able to get both the contacts first and last name separately, I have tried to use the StructuredName in an attempt to get this but I can't seem to get it working.

Can anyone point me in the right direction as to how to use the StructuredName correctly to get the name split into First and Last?

UPDATE:

Following hovanessyan's advice I have attempted the following:

    String[] PROJECTION = new String[] {
            ContactsContract.Data._ID,
            ContactsContract.Data.DISPLAY_NAME,
            ContactsContract.Data.HAS_PHONE_NUMBER,
    };

    String SELECTION = ContactsContract.CommonDataKinds.StructuredName.IN_VISIBLE_GROUP + " = '1'";

    String sortOrder = ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, sortOrder);

    int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
    int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);

    while (cursor.moveToNext()) {
        String given = cursor.getString(indexGivenName);
        String family = cursor.getString(indexFamilyName);
        String display = cursor.getString(indexDisplayName);
        Log.e("XXX", "Name: | " + given + " | " + family + " | " + display);
    } 

However using the PROJECTION causes a crash as follows:

12-08 16:52:01.575: E/AndroidRuntime(7912): Caused by: java.lang.IllegalArgumentException: Invalid column has_phone_number

If I remove the PROJECTION I get all the results printed out but a lot of them contain NULL.

For example:

12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null

So can anyone see what I'm doing wrong in that my PROJECTION doesn't work?

FURTHER UPDATE:

I have sorted out the issues with my PROJECTION but now I have an issue where the DATA content provider is supplying me back with all the null data and causing NULL pointer exceptions in my code.

A cursor count from ContactsContract.Contacts gives me back 115 for example but using the DATA table gives me back 464 using the same parameters and this is causing huge issues in my app.

Anyone have any ideas why that is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别把无礼当个性 2024-12-27 03:11:09

看一下 ContactsContract.CommonDataKinds.StructuredName 类。那里有所有需要的列,您可能可以执行以下操作:

Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, other_query_params_for_filtering);

int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);

while (cursor.moveToNext()) {
    String given = cursor.getString(indexGivenName);
    String family = cursor.getString(indexFamilyName);
    String display = cursor.getString(indexDisplayName);
}

Take a look at ContactsContract.CommonDataKinds.StructuredName class. You have all the needed columns there, and you can probably do something like:

Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, other_query_params_for_filtering);

int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);

while (cursor.moveToNext()) {
    String given = cursor.getString(indexGivenName);
    String family = cursor.getString(indexFamilyName);
    String display = cursor.getString(indexDisplayName);
}
毁梦 2024-12-27 03:11:09

以下是从 ContactsContract.Data 表获取用户数据的通用函数:

Map<String, String> result = new HashMap<>();
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + "='" + YOUR_CONTACT_ID + "'", null, null);
if (cursor != null) {
    while (cursor.moveToNext()) {
        String mime = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
        switch (mime) {
            case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE:
                result.put(FIRST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)));
                result.put(LAST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)));
                break;
            case ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE:
                result.put(CITY, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
                result.put(STREET, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
                result.put(ZIP, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
                break;
            case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
                if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))) {
                    result.put(MOBILE, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                }
                break;
        }
    }
    cursor.close();
}
return result;

Here is a general function for getting user data from ContactsContract.Data table:

Map<String, String> result = new HashMap<>();
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + "='" + YOUR_CONTACT_ID + "'", null, null);
if (cursor != null) {
    while (cursor.moveToNext()) {
        String mime = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
        switch (mime) {
            case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE:
                result.put(FIRST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)));
                result.put(LAST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)));
                break;
            case ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE:
                result.put(CITY, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
                result.put(STREET, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
                result.put(ZIP, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
                break;
            case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
                if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))) {
                    result.put(MOBILE, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                }
                break;
        }
    }
    cursor.close();
}
return result;
我一直都在从未离去 2024-12-27 03:11:09
        Cursor phone_cursor = cr.query(ContactsContract.CommonDataKinds.
                Phone.CONTENT_URI, null, null, null, null);
        while (phone_cursor.moveToNext()) {
            try {
            int id = Integer.parseInt(phone_cursor.getString(phone_cursor.getColumnIndex
                    (ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
            Cursor name_cursor = cr.query(ContactsContract.Data.CONTENT_URI,null,
                    ContactsContract.Data.CONTACT_ID + "  = " + id, null, null);

                String name = phone_cursor.getString(phone_cursor.getColumnIndex
                        (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String first_name ="";
                String last_name = "";
                while (name_cursor.moveToNext()) {
                    if(name_cursor.getString(name_cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))!=null){
                    first_name = name_cursor.getString(name_cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    last_name = name_cursor.getString(name_cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                }}
                name_cursor.close();
                String phoneNumber = phone_cursor.getString(phone_cursor.getColumnIndex
                        (ContactsContract.CommonDataKinds.Phone.NUMBER));
            } catch (Exception e) {
            }
        }
        phone_cursor.close();
        Cursor phone_cursor = cr.query(ContactsContract.CommonDataKinds.
                Phone.CONTENT_URI, null, null, null, null);
        while (phone_cursor.moveToNext()) {
            try {
            int id = Integer.parseInt(phone_cursor.getString(phone_cursor.getColumnIndex
                    (ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
            Cursor name_cursor = cr.query(ContactsContract.Data.CONTENT_URI,null,
                    ContactsContract.Data.CONTACT_ID + "  = " + id, null, null);

                String name = phone_cursor.getString(phone_cursor.getColumnIndex
                        (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String first_name ="";
                String last_name = "";
                while (name_cursor.moveToNext()) {
                    if(name_cursor.getString(name_cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))!=null){
                    first_name = name_cursor.getString(name_cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    last_name = name_cursor.getString(name_cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                }}
                name_cursor.close();
                String phoneNumber = phone_cursor.getString(phone_cursor.getColumnIndex
                        (ContactsContract.CommonDataKinds.Phone.NUMBER));
            } catch (Exception e) {
            }
        }
        phone_cursor.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文