开发者

retrieve contact's nickname

I want to get the nickname of a contact from addressbook. I start with his phone number, query it and want the nickname (aka alias) as a result.

Cursor cur = context.getContentResolver().query(ContactsContract.Comm开发者_C百科onDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + incomingNumber, null, null);

        if (cur.moveToFirst()) {
            Log.e("saymyname", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)));
            Log.e("saymyname", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.LABEL)));
        }

Output of the logs is the incomingNumber (first Log.e() ) and null (second Log.e() ), but I want to get the contact's nickname!

Thanks Tom


Nickname is held in a different table than the phone numbers, you have to query ContactsContract.Data.CONTENT_URI

Check my answer on this question


(I don't have necessary reputation to comment so I have to add answer)

TomTasche's answer is misleading and it got me to waste a lot of time trying to figure out why I couldn't get the proper nickname on a contact I knew had one.

I found the answer by myself but got the confirmation from this post that I was now doing it properly.

Basically when you read the ContactsContract.Data documentation you read:

Each row of the data table is typically used to store a single piece of contact information (such as a phone number) and its associated metadata (such as whether it is a work or home number).

That explains the shady part of TomTasche's code :

if (nickname.equals(incomingNumber)) {
                return name;
            }

Since doing a search with just the CONTACT_ID can return multiple rows (one for each information type), it's not guaranteed that the first one contains the nickname. When there is a nickname it will be in DATA1, but the phone number is also found in DATA1.

The example part in the documentation of ContactsContract.Data makes it clear that rows must be selected based on their MIME_TYPE, only when the MIME_TYPE is selected, can we start making sense of the content in the row itself.

A proper query would therefore be:

cursor =  getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[]{Nickname.NAME},
                ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "= ?",
                new String[]{contactID, Nickname.CONTENT_ITEM_TYPE},
                null);

(where Nickname is ContactsContract.CommonDataKinds.Nickname)

I felt I had to say something on this topic to prevent other people wasting as much time as I did based on this sole topic (first one I found with my Google friend).


The answer from Pentium10 was very helpful! Thanks!

If anybody needs a sample, look at the following code:

public String accessContact(String incomingNumber) {
        if (incomingNumber == null || "".equals(incomingNumber)) {
            return "unknown";
        }

        try {
            Cursor cur = context.getContentResolver().query(Phone.CONTENT_URI, new String[] {Phone.DISPLAY_NAME, Phone.TYPE, Phone.CONTACT_ID}, Phone.NUMBER + " = " + incomingNumber, null, null);

            int nameIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int typeIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
            int idIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);

            String name;
            String type;
            String id;

            if (cur.moveToFirst()) {
                name = cur.getString(nameIndex);
                type = cur.getString(typeIndex);
                id = cur.getString(idIndex);
            } else {
                return "unknown";
            }

            cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {ContactsContract.Data.DATA1}, ContactsContract.Data.CONTACT_ID + " = " + id, null, null);

            int nicknameIndex = cur.getColumnIndex(ContactsContract.Data.DATA1);
            String nickname;

            if (cur.moveToFirst()) {
                nickname = cur.getString(nicknameIndex);
                if (nickname.equals(incomingNumber)) {
                    return name;
                }
                return nickname;
            } else {
                return name;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "unknown";
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜