Android - get alias and alias type

I am trying to get a contact nickname for several hours and still cannot get them to work, they told me that they are in the differt table by phone numbers, etc. But I do not know how to access them.

The closest I have is ...

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 contact data along with the type eg: Thomas Owers 1 , it's all good, but it does not give me the data, so it gives the email address, phone number, names, nickname but I can not distinguish between them.

Any help would be greatly appreciated, thanks! :)

+1
source share
2 answers

I managed to get an alias after hours of searching the web ...

 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 an alias! :)

+5
source

Do not get all the data. only get the required data using the 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
source

Source: https://habr.com/ru/post/1312972/


All Articles