Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Relationship contact field on Android

I am working with Androids contacts and trying to get particular pieces of data. I can already get emails, phone numbers, their name, etc. However I am having difficulty getting the relationship field.

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

So my goal is: Given a particular userid (from the contact database on Android), figure out their relation field.

like image 721
Coltin Avatar asked Oct 08 '22 18:10

Coltin


1 Answers

This should work. The idea is to connect search in the Data table but use stuff from CommonDataKinds. This is done in where clause ... Data.MIMETYPE == CommonDataKinds.Relation.CONTENT_ITEM_TYPE. This will get you the row with all the Relation stuff.

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();
    }
}
like image 121
giZm0 Avatar answered Oct 10 '22 23:10

giZm0