0

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 table. For example by using contact id, I have to query separately to phone, email and address tables for each contact which I feel is not much efficient. It would be really helpful if someone could point me out ways to get all the contact details in a single query. Thanks in advance :)

iPhoneCoder
  • 237
  • 1
  • 3
  • 11

2 Answers2

0

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.

hovanessyan
  • 30,580
  • 6
  • 55
  • 83
0

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

private interface DataQuery {
    public static final String[] PROJECTION = new String[] { Data._ID, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3, };

    public static final int COLUMN_ID = 0;
    public static final int COLUMN_MIMETYPE = 1;
    public static final int COLUMN_DATA1 = 2;
    public static final int COLUMN_DATA2 = 3;
    public static final int COLUMN_DATA3 = 4;
    public static final int COLUMN_PHONE_NUMBER = COLUMN_DATA1;
    public static final int COLUMN_PHONE_TYPE = COLUMN_DATA2;
    public static final int COLUMN_GIVEN_NAME = COLUMN_DATA2;
    public static final int COLUMN_FAMILY_NAME = COLUMN_DATA3;

    public static final String SELECTION = Data.RAW_CONTACT_ID + "=?";
}


final Cursor c = resolver.query(Data.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION, new String[] { String.valueOf(rawContactId) }, null);

try {
    while (c.moveToNext()) {
        final long id = c.getLong(DataQuery.COLUMN_ID);
        final String mimeType = c.getString(DataQuery.COLUMN_MIMETYPE);
        uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);

        if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
            final String oldLastName = c.getString(DataQuery.COLUMN_FAMILY_NAME);
            final String oldFirstName = c.getString(DataQuery.COLUMN_GIVEN_NAME);
            //store them where you need
        } else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
            final int type = c.getInt(DataQuery.COLUMN_PHONE_TYPE);
            final String cellPhone = 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

kingston
  • 11,053
  • 14
  • 62
  • 116
  • Hi herschel. Thanks for the reply. Do you mean to say that I can get the email, phone, address in a single query using Data.CONTENT_URI? if yes can you please explain it a bit further how it is possible to do so? Thanks again :) – iPhoneCoder Dec 19 '11 at 06:46