0

I need to add a contact in a contacts group. The group is added but the contact is not added in the group.
Here is my code. I think the exception occurs in

ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID

but i don't know why.. Can you can help me please?

ops.add(ContentProviderOperation
        .newInsert(ContactsContract.RawContacts.CONTENT_URI)
        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,accountType)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME,accountName)
        .build());

ops.add(ContentProviderOperation
        .newAssertQuery(ContactsContract.Groups.CONTENT_URI)
        .withValueBackReference(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 0)
        .withSelection(ContactsContract.Groups.TITLE + "=?", new String[]{grupo})
        .withExpectedCount(1)
        .build());

ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE,           
                   ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, nome)
        .build());

ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.
                   CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Email.DATA1, mail)  
        .build());

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

try {
    resolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch(Exception e) {
    e.printStackTrace();
}       
ekad
  • 14,436
  • 26
  • 44
  • 46
Ricardo Graça
  • 581
  • 2
  • 9
  • 17

1 Answers1

0

The person who wrote the method you're calling incorrectly decided to throw that exception if you passed in any data that it couldn't handle. I'd recommend consulting the javadocs to see if there's an explanation there.

It's called checking pre-conditions; programming by contract.

duffymo
  • 305,152
  • 44
  • 369
  • 561