Skip to content Skip to sidebar Skip to footer

Best Way To Access All The Details Of Android Contacts

I am writing an application to sync contact details with my server. For this I need to query Contacts using content provider multiple times as all the data is not present in one ta

Solution 1:

If you have the rawContactId you don't need multiple query. You can have a single query with Data.CONTENT_URI as uri and with a selection on your rawContactId.

The you have to loop with the resulting cursor to read the info. To know in wich column you need to see for a given row in the Data table you need to check the MIMETYPE

EDIT

privateinterfaceDataQuery {
    publicstaticfinal String[] PROJECTION = newString[] { Data._ID, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3, };

    publicstaticfinalintCOLUMN_ID=0;
    publicstaticfinalintCOLUMN_MIMETYPE=1;
    publicstaticfinalintCOLUMN_DATA1=2;
    publicstaticfinalintCOLUMN_DATA2=3;
    publicstaticfinalintCOLUMN_DATA3=4;
    publicstaticfinalintCOLUMN_PHONE_NUMBER= COLUMN_DATA1;
    publicstaticfinalintCOLUMN_PHONE_TYPE= COLUMN_DATA2;
    publicstaticfinalintCOLUMN_GIVEN_NAME= COLUMN_DATA2;
    publicstaticfinalintCOLUMN_FAMILY_NAME= COLUMN_DATA3;

    publicstaticfinalStringSELECTION= Data.RAW_CONTACT_ID + "=?";
}


finalCursorc= resolver.query(Data.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION, newString[] { String.valueOf(rawContactId) }, null);

try {
    while (c.moveToNext()) {
        finallongid= c.getLong(DataQuery.COLUMN_ID);
        finalStringmimeType= c.getString(DataQuery.COLUMN_MIMETYPE);
        uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);

        if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
            finalStringoldLastName= c.getString(DataQuery.COLUMN_FAMILY_NAME);
            finalStringoldFirstName= c.getString(DataQuery.COLUMN_GIVEN_NAME);
            //store them where you need
        } elseif (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
            finalinttype= c.getInt(DataQuery.COLUMN_PHONE_TYPE);
            finalStringcellPhone= c.getString(DataQuery.COLUMN_PHONE_NUMBEIR);
            //store them where you need
            }
        }
    } // while
} finally {
    if (c!=null) c.close();
}

Please consider that I did not check the code: I do not have a compiler here. I hope it will be useful anyway

Solution 2:

Data is a generic table that can hold any kind of contact data.

The kind of data stored in a given row is specified by the row's MIMETYPE value, which determines the meaning of the generic columns DATA1 through DATA15.

For example, if the data kind is Phone.CONTENT_ITEM_TYPE, then the column DATA1 stores the phone number, but if the data kind is Email.CONTENT_ITEM_TYPE, then DATA1 stores the email address. Sync adapters and applications can introduce their own data kinds.

Post a Comment for "Best Way To Access All The Details Of Android Contacts"