我不确定这些组,但这是我用于其余部分的:
(阅读到底部以获得解释)
声明:
static ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
动作代码:
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;
}
填充代码:
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;
}
这段代码基本上用几个值填充了一个大型 ArrayList 操作,以插入到联系人数据库中。
您需要确保使用 .withValueBackReference(opsLength) 以便您指向正确的原始联系人。
此代码经过测试,可在运行 2.2 的 HTC Incredible 上运行。
祝你好运!