I'm not sure about the groups, but this is what I used for the rest:
(READ TO BOTTOM FOR EXPLANATION)
Declarations:
static ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Action code:
ops.addAll(fillContentProviderOperation(accounts, ctaList,
ops));
private ArrayList<ContentProviderOperation> fillContentProviderOperation(
Account[] accounts, ArrayList<ContactToAdd> ctaList,
ArrayList<ContentProviderOperation> privateOps) //
{
for (int i = 0; i < ctaList.size(); i++) //
{
if (ctaList.get(i) != null) //
{
if (ctaList.get(i).LastName != ""
&& ctaList.get(i).LastName != null) //
{
privateOps.addAll(addToContacts(ctaList.get(i),
privateOps.size(), accounts,
ctaList.get(i).groupType));
publishProgress();
}
}
}
return privateOps;
}
Filler code:
protected ArrayList<ContentProviderOperation> addToContacts(
ContactToAdd cta, int opsLength, Account[] accounts, String groupName) //
{
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, accounts[0].type)
.withValue(RawContacts.ACCOUNT_NAME, accounts[0].name).build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.GIVEN_NAME, cta.FirstName)
.withValue(StructuredName.FAMILY_NAME, cta.LastName).build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, cta.DayWorkPhoneNumber.PhoneNumber)
.withValue(Phone.TYPE, Phone.TYPE_MOBILE).build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.DATA1, cta.Email)
.withValue(Email.TYPE, Email.TYPE_MOBILE).build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(StructuredPostal.STREET, cta.MailingAddress.Address1)
.withValue(StructuredPostal.CITY, cta.MailingAddress.City)
.withValue(StructuredPostal.REGION,
cta.MailingAddress.StateCode)
.withValue(StructuredPostal.POSTCODE,
cta.MailingAddress.PostalCode)
.withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME)
.build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(StructuredPostal.STREET,
cta.ShippingAddress.Address1)
.withValue(StructuredPostal.CITY, cta.ShippingAddress.City)
.withValue(StructuredPostal.REGION,
cta.ShippingAddress.StateCode)
.withValue(StructuredPostal.POSTCODE,
cta.ShippingAddress.PostalCode)
.withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK)
.build());
return ops;
}
This code basically fills a large ArrayList ops with several values to insert into the contacts database.
You need to make sure you use the .withValueBackReference(opsLength) so you are pointing BACK at the correct raw contact.
This code is tested and works on an HTC Incredible running 2.2.
Good luck!