问题
I have this code to delete a Contact Group
public void delete(Activity act,String[] args) {
try {
int b=act.getContentResolver().delete(ContactsContract.Groups.CONTENT_URI,"_ID=?", args);
Toast.makeText(act, "Deleted",Toast.LENGTH_SHORT).show();
//notify registered observers that a row was updated
act.getContentResolver().notifyChange(ContactsContract.Groups.CONTENT_URI, null);
} catch (Exception e) {
Log.v(TAG, e.getMessage(), e);
Toast.makeText(act, TAG + " Delete Failed",Toast.LENGTH_LONG).show();
}
}
I call the method like
private void processDelete(long rowId) {
String[] args = { String.valueOf(rowId) };
objItem.delete(this, args);
cur.requery();
}
I have
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
The ID is passed ok.
The b value returns 1, but the delete is not performed, on activity restart I still see the record in the list. What I am doing wrong?
回答1:
You don't need to add where clause saying that. If you wont to instantly delete items in database and not flag them as deleted, add this row to you URI.
mUri.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER,"1").build();
//and then call your delete function with URI appended like this.
mResolver.delete(mUri,null, null);
that flag: CALLER_IS_SYNCADAPTER set to 1 will delete row instantly.
Hope this helped.
回答2:
It was my miss:
When quering the existing records I had to add a where clause to denote that I do not want deleted=1
values, as the values are not delete instantly, they are flagged as deleted.
Cursor managedCursor = act.managedQuery(contacts, projection,
ContactsContract.Groups.DELETED + "=0",
null,
ContactsContract.Groups.TITLE + " ASC");
return managedCursor;
回答3:
you can delete contact group using this
private void deletaAllInGroup(Context context, long groupId)
throws RemoteException, OperationApplicationException{
ContentValues values = new ContentValues();
values.put(ContactsContract.Groups._ID, groupId);
getContentResolver().delete(ContactsContract.Groups.CONTENT_URI,values.toString(),null);
}
来源:https://stackoverflow.com/questions/2353015/issue-using-contact-group-delete-on-android