我有此代码删除联系人组
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();
}
}
我称之为
的方法private void processDelete(long rowId) {
String[] args = { String.valueOf(rowId) };
objItem.delete(this, args);
cur.requery();
}
我有
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
ID传递正确。
b值返回1,但是没有执行删除,在活动重启时我仍然看到列表中的记录。 我做错了什么?
答案 0 :(得分:2)
您不需要添加where子句。 如果您不想立即删除数据库中的项目而不将其标记为已删除,请将此行添加到您的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);
该标志:CALLER_IS_SYNCADAPTER设置为1将立即删除行。
希望这会有所帮助。
答案 1 :(得分:1)
这是我的遗憾:
当查询现有记录时,我必须添加一个where子句来表示我不想要deleted=1
值,因为值不会立即删除,它们会被标记为已删除。
Cursor managedCursor = act.managedQuery(contacts, projection,
ContactsContract.Groups.DELETED + "=0",
null,
ContactsContract.Groups.TITLE + " ASC");
return managedCursor;
答案 2 :(得分:1)
您可以使用此
删除联系人群组 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);
}