Search code examples
androidkotlinandroid-contactsandroid-contentresolver

How to update multiple groups to contact on android?


I just success to insert multiple group to a contact, but failed to update multiple group on a contact. my code just update only 1 group, not all the list of groupsId (see below)

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

I have tried this code to :

groupsId.forEach {

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

the code that i success create new contact with multiple group.

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()
    )

Any helps is appreciated.


Solution

  • actually i got wrong id contact for insert it, I should use raw contact Id with this function.

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

    then insert it (for adding the contact's groups), not using update. because update is only set the group only 1 group.

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

    If you want to clear the previous contact's group and insert more than one. It's should update it first (1 group) to remove previous, then insert the other groups.

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