Failure to delete entire contact using ContentProviderOperation

一个人想着一个人 提交于 2020-01-15 02:37:28

问题


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 delete 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


回答1:


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.



来源:https://stackoverflow.com/questions/3058622/failure-to-delete-entire-contact-using-contentprovideroperation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!