Sudhir
Sudhir

Reputation: 805

Get the Max _id from ContentResolver from Contacts.CONTENT_URI

I want to get the last added contact in the contacts and for getting that i want to pull the max _id of the contact. So here is my query that i want to realise:

AppMain.applicationContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
    new String[]{
            "MAX(" + ContactsContract.Contacts._ID + ") as max_id", 
            ContactsContract.Contacts.DISPLAY_NAME, 
            ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER
    }, 
    null, null, null);

But unfortunately i am getting this error: Invalid column MAX(contact_id) as max_id

I tried removing the 'as max_id' but no luck.

Does any one know how to get the last added contact or get the max _id of the contact.

Upvotes: 0

Views: 313

Answers (1)

marmor
marmor

Reputation: 28199

This is not officially supported by Android's ContentProvider framework, especially if the Provider set the strictProjectionMap flag.

But this should work instead, it asks for all contacts sorted by contact-id, and limits the results to 1:

Cursor c = cr.query(Contacts.CONTENT_URI, new String[] { Contacts._ID }, null, null, Contacts._ID + "DESC LIMIT 1");
if (c != null && c.moveToNext()) {
    Log.d(TAG, "id is: " + c.getLong(0));
}

Upvotes: 1

Related Questions