开发者

Failure to delete entire contact using ContentProviderOperation

I have been working with Android contacts. I am able to show them, update but when I want to delete any, it is not deleted completely. In Contacts application is shown as (Unknown) without any data. Here is my example:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(Data.CONTACT_ID + "=?", new String[]{selectedid})
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

Should I do anything else to delete contact entirely? It seems that these code delete info in table ContactsContract.Data but it does not dele开发者_如何学Pythonte element related in table ContactsContract.Contacts or ContactsContract.RawContacts.how can i do to delete an contact completely?

============================================================================

also, i tried deprecated method. It dose work, but i do not want to do so. Here is the sample code:

ContentResolver contentResolver = m_cContent.getContentResolver();
contentResolver.delete(People.CONTENT_URI, People.NAME + "=?", new String[] { SelectedName });

and if i modify this code to

ContentResolver contentResolver = m_cContent.getContentResolver();
contentResolver.delete(ContactsContract.Contacts, ContactsContract.Contacts._ID + "=?", new String[] { Selectedid });

It has no effect.

Does it mean that one can only delete a contact by name instead of by its id? What on earth can i do to delete contact?

Thanks, Enchor


You are trying to delete a contact by deleting its data rows from the Data table. That wouldn't work.

A contact is made up of several raw-contacts, each raw-contact has its data saved in the Data table. When deleting a contact, all raw-contacts get deleted as well, along with their data.

Do this:

long contactId = 12345;
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, String.valueOf(contactId));
int deleted = getContentResolver().delete(contactUri, null, null);

deleted will be 1 if the operation succeeded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜