我不知道有關的團體,但是這是我剩下的部分使用:
(READ到底部的解釋)
聲明:
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 ops,並插入到聯繫人數據庫中。
您需要確保使用.withValueBackReference(opsLength),以便您將BACK指向正確的原始聯繫人。
此代碼已經過測試,可以在HTC Incredible 2.2上運行。
祝你好運!