Reputation: 3070
I want to insert a new phone number with new contact id in the my SQLite. I need to know the maximum contact id in my contact, to ignore the saving same contact id. Is it possible to know the maximum contact id in Android? Thank you
Upvotes: 1
Views: 62
Reputation: 1694
From Contacts Table , you can get the last Contact Id by querying like shown below
final String sortOrder = ContactsContract.Contacts._ID+" DESC LIMIT 1";
Cursor lcursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,new String[] {ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME},null,null,sortOrder);
if (lcursor != null && lcursor.moveToFirst()) {
long id = lcursor.getLong(lcursor.getColumnIndex(ContactsContract.Contacts._ID));
String displayName = lcursor.getString(lcursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
Cursor will return only one entry and that entry will be the last contact_id in the contact's table
Note: This should not be done on Main thread, as it may take too much time if device Contact is more 1000+ contacts, so always be carefull of Querying the DataBase having large entries
Enjoy coding ! :)
Upvotes: 2