My application uses the Contacts ContentProvider
to store some of its data. When I load a contact into memory, I want to save its ID
(so that I know how to save changes later), and the ID
of all data fields it is using (so they can be directly updated). Here is some of my code:
Uri entityUri = Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, id),
Entity.CONTENT_DIRECTORY);
Cursor resultData = context.getContentResolver().query(
entityUri,
new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
null, null, null);
resultData.moveToFirst();
this.id = id;
while (resultData.isAfterLast() == false) {
this.source_id = resultData.getInt(0);
if (!resultData.isNull(1)) {
if (resultData.getString(2).equals(Fields.DISPLAY_NAME)) {
this.display_name = resultData.getString(3);
this.display_name_id = resultData.getInt(1);
}
}
resultData.moveToNext();
}
resultData.close();
return this;
That queries the ContentProvider
and gets the DISPLAY_NAME
field from the data. The ID
of the data record is stored in the display_name_id
variable. It comes out as 4612
when I run it on my device.
I tried saving it, but it does not update as expected. In order to debug, I added a query that tries to find the correct data table record.
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.Data._ID, CommonDataKinds.StructuredName.DISPLAY_NAME},
ContactsContract.Data._ID + "=?",
new String[] {String.valueOf(this.display_name_id)}, null);
However, this cursor comes back as having a length of 0
. How can this be? Why is the ID incorrect?
I tried locally and it works for me, here's my code slightly adapted from yours:
There's no big difference from your code, so compare or try to replace bits of it to see where you have a problem. Make sure you use a fresh
Cursor
for each query, and close it correctly afterwards (in afinally
clause).Another thing, make sure that
if (resultData.getString(2).equals(Fields.DISPLAY_NAME))
is really what you're wanting to do (it compares the entry mime type withFields.DISPLAY_NAME
), but since you're saying you get the data ID correctly this shouldn't be the problem.