简体   繁体   中英

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.

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM