获取联系人的姓氏和名字,而不是单一的显示名称? [英] Get first and last name of a contact rather than single display name?

查看:214
本文介绍了获取联系人的姓氏和名字,而不是单一的显示名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正与Android的通讯录内容提供商,目前可以使用下面的code使用没有问题,联系人完整的显示名称:

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

不过,我希望能够得到这两个触点第一和分开,我曾尝试使用StructuredName,试图获得这最后的名字,但我似乎无法得到它的工作。

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.

任何人都可以点我在正确的方向,如何使用StructuredName正确地获得这个名字分成第一和最后?

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?

更新:

随着hovanessyan的意见,我已经尝试了以下内容:

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

如果我删除投射我得到的所有结果打印出来,但他们中的很多包含NULL。

If I remove the PROJECTION I get all the results printed out but a lot of them contain 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

所以,任何人都可以看到我在做什么错,我的投影不起作用?

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

进一步的更新:

我已经整理出来与我的投影问题,但现在我有一个问题,即数据内容提供商提供我回来的所有空数据并导致空指针异常在我的code。

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.

从ContactsContract.Contacts光标计数给我回115的例子,但使用的数据表使用相同的参数给我回464,这是导致我的应用程序的巨大问题。

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?

推荐答案

看看<一href="http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.StructuredName.html">ContactsContract.CommonDataKinds.StructuredName类。你拥有所有需要的列那里,你也许可以这样做:

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

这篇关于获取联系人的姓氏和名字,而不是单一的显示名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆