如何更新多个组以在 android 上联系?

How to update multiple groups to contact on android?

我刚刚成功将多个组插入一个联系人,但未能更新一个联系人的多个组。我的代码只更新 1 个组,而不是所有 groupsId 列表(见下文)

    private fun updateGroupOnContact(
        operations: ArrayList<ContentProviderOperation>,
        where: String,
        args: Array<String>,
        groupsId: Array<String>
    ) {
        val values = ContentValues()

        groupsId.forEach {
            val newVal = ContentValues()
            newVal.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, it)
            values.putAll(newVal)
        }

        operations.add(
            ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(where, args)
                .withValues(values)
                .build()
        )
    }

我试过这段代码:

groupsId.forEach {

        operations.add(
            ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(where, args)
            .withValue(
                ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, it
            )
                .build()
        )
    }

我成功的代码创建新联系人与多个组。

contentProvideOperation.add(
        ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(
                ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,
                groupID
            )
            .withValue(
                ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,
                ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
            )
            .build()
    )

感谢任何帮助。

实际上我插入错误的联系人 ID,我应该使用此功能的原始联系人 ID。

private fun getRawContactId(contactId: String): String? {
        val projection = arrayOf(RawContacts._ID)
        val selection = RawContacts.CONTACT_ID + "=?"
        val selectionArgs = arrayOf(contactId)
        val c: Cursor = contentResolver?.query(
            RawContacts.CONTENT_URI,
            projection,
            selection,
            selectionArgs,
            null
        )
            ?: return null
        var rawContactId = -1
        if (c.moveToFirst()) {
            rawContactId = c.getInt(c.getColumnIndex(RawContacts._ID))
        }
        c.close()
        return rawContactId.toString()
    }

然后插入它(用于添加联系人的组),而不是使用更新。 因为更新只设置了组只有1组。

private fun insertGroupOnContact(
            operations: ArrayList<ContentProviderOperation>,
            contactId: String,
            groupId: String
        ) {
            ContentProviderOperation.newInsert(Data.CONTENT_URI).apply {
                withValue(Data.RAW_CONTACT_ID, getRawContactId(contactId))
                withValue(GroupMembership.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE)
                withValue(GroupMembership.GROUP_ROW_ID, groupId)
                operations.add(build())
            }
        }

如果你想清除之前联系人的群组并插入多个。它应该首先更新它(1 组)以删除以前的组,然后插入其他组。

private fun updateGroupOnContact(
    operations: ArrayList<ContentProviderOperation>,
    where: String,
    args: Array<String>,
    groupId: String
) {
    operations.add(
        ContentProviderOperation.newUpdate(Data.CONTENT_URI)
            .withSelection(where, args)
            .withValue(GroupMembership.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE)
            .withValue(GroupMembership.GROUP_ROW_ID, groupId)
            .build()
    )
}