在 Android 上使用联系人组删除时出现问题

发布于 2024-08-24 01:29:18 字数 1025 浏览 6 评论 0原文

我有这段代码来删除联系人组,

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,但未执行删除,活动重新启动时我仍然在列表中看到该记录。 我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如日中天 2024-08-31 01:29:18

您不需要添加 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 将立即删除行。

希望这有帮助。

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.

请你别敷衍 2024-08-31 01:29:18

这是我的失误:

在查询现有记录时,我必须添加一个 where 子句来表示我不想要 deleted=1 值,因为这些值不会立即删除,它们会被标记为已删除。

Cursor managedCursor = act.managedQuery(contacts, projection, 
                ContactsContract.Groups.DELETED + "=0", 
                null,
                ContactsContract.Groups.TITLE + " ASC");
        return managedCursor;

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;
书信已泛黄 2024-08-31 01:29:18

您可以使用此删除联系人组

    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);
        }

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);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文