开发者

Android - Get Nickname and Type of Nickname

I've been trying to get the nickname of a contact for a few hours now, and still cant get them to work, I've been told that they are in a differnt table to the phone numbers etc.. But i dont know how to access them.

The closest I've got is this..

Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = ?", new String[]{String.valueOf(recordId)}, null);
    while (cursor.moveToNext()) {
       Cursor nickname = context.getContentResolver().query( ContactsContract.Data.CONTENT_URI, null, ContactsContract.CommonDataKinds.Nickname.CONTACT_ID +" = "+ recordId, null, null); 
       while (nickname.moveToNext()) { 
           try {
           String nicknameName = nickname.getString(nickname.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
           String nicknameType = nickname.getString(nickname.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.TYPE));

           switch (Integer.valueOf(nicknameType)) {
           case 1: nicknameType = "TYPE_HOME"; break;
           }
           list.add(new KeyValue("Nickname:" + nicknameType, nicknameName));
           } catch开发者_开发知识库 (Exception e) { continue; }
       }
       nickname.close();
    }

This gets all the data of the contact along with the type eg: Thomas Owers 1 this is all good but it doesn't give me what the data is, so it gives the email, phone, names, nickname but i cant distinguish between them.

Any help would be much apreciated, Thank you! :)


I was able to get the nickname after several hours of searching the internet...

ArrayList<KeyValue> list = new ArrayList<KeyValue>();

    Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = ?", new String[]{String.valueOf(recordId)}, null);
    while (cursor.moveToNext()) {
       String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
       String[] params = new String[] {String.valueOf(recordId), ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE};
       Cursor nickname = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null); 
       while (nickname.moveToNext()) { 
            String nicknameName = nickname.getString(nickname.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
            String nicknameType = nickname.getString(nickname.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.TYPE));
            switch (Integer.valueOf(nicknameType)) {
            case 1: nicknameType = "Default"; break;
            case 2: nicknameType = "OtherName"; break;
            case 3: nicknameType = "MaidenName"; break;
            case 4: nicknameType = "ShortName"; break;
            case 5: nicknameType = "Initials"; break;
            }
            list.add(new KeyValue("Nickname:" + nicknameType, nicknameName));
       }
       nickname.close();
    }
    return list;

This code gets the nickname! :)


Donot get all data. only get the required data using projection

String[] proj ={ContactsContract.CommonDataKinds.Nickname.NAME, ContactsContract.CommonDataKinds.Nickname.TYPE};

Cursor nickname = getContentResolver().query( ContactsContract.Data.CONTENT_URI, proj,ContactsContract.CommonDataKinds.Nickname.CONTACT_ID +" = "+ recordId, null, null); 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜