Contact Android Contact Field

I work with Androids contacts and try to get individual pieces of data. I can already receive emails, phone numbers, their name, etc. However, it’s hard for me to get a relationship field.

http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Relation.html

So, my goal: given a specific user ID (from the contacts database on Android), find out their relationship field.

+4
source share
1 answer

That should work. The idea is to join a search in a data table, but use material from CommonDataKinds. This is done where the sentence ... Data.MIMETYPE == CommonDataKinds.Relation.CONTENT_ITEM_TYPE . This will give you a line with all Relation materials.

 import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract.CommonDataKinds.Relation; import android.provider.ContactsContract.Data; import android.util.Log; ... public void logCatTheRelation(long contactId){ Uri uri = Data.CONTENT_URI; String where = String.format( "%s = ? AND %s = ?", Data.MIMETYPE, Relation.CONTACT_ID); String[] whereParams = new String[] { Relation.CONTENT_ITEM_TYPE, Long.toString(contactId), }; String[] selectColumns = new String[]{ Relation.NAME, // add additional columns here }; Cursor relationCursor = this.getContentResolver().query( uri, selectColumns, where, whereParams, null); try{ if (relationCursor.moveToFirst()) { Log.d("gizm0", relationCursor.getString( relationCursor.getColumnIndex(Relation.NAME))); } Log.d("gizm0", "sadly no relation ... "); }finally{ relationCursor.close(); } } 
+3
source

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


All Articles