gpt4 book ai didi

java - 在 Android 2.3.5 中插入多个联系人

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:53 25 4
gpt4 key购买 nike

我正在尝试从我的应用程序中添加多个联系人。我正在从一个 xml 文件中读取数据,然后将其包装到一个强类型对象列表中。然后遍历这些对象,我正在为每个对象调用插入到用户联系人中。

然而,我在测试时得到了一些非常奇怪的结果,我得到了 1 个或两个工作联系人,然后是一些空的(未知)条目。 (Unknowns) 的成功条目数似乎是随机的,但我不知道为什么我不能执行一些应该相当简单的事情。

我的点击事件启动命令来获取数据,然后将其添加到联系人如下。我可以确认数据每次都被读取到联系人列表中,所以这不是数据问题。

public void onClick(View v) {
List<AddressContent> contacts = GetAddresses();
int counter = 0;

if (!contacts.isEmpty()) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

for (int i = 0; i < contacts.size(); i++) {

ops.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,
null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,
null)
.withValue(ContactsContract.RawContacts.AGGREGATION_MODE,
ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED).build());

// ------------------------------------------------------ Name

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
contacts.get(i).getName()).build());

// ------------------------------------------------------ Work
// Number

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
contacts.get(i).getPhone())
.withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.build());

// ------------------------------------------------------ Fax
// Number

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
contacts.get(i).getFax())
.withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK)
.build());

// ------------------------------------------------------ Email

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA,
contacts.get(i).getEmail())
.withValue(
ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());

// ------------------------------------------------------
// Organisation

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Organization.COMPANY,
contacts.get(i).getCompany())
.withValue(
ContactsContract.CommonDataKinds.Organization.TYPE,
ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
.build());

// ------------------------------------------------------
// Address

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
contacts.get(i).getStreet())
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
contacts.get(i).getCity())
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
contacts.get(i).getPostcode())
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
contacts.get(i).getCountry())
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
.build());
counter++;
}

try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY,
ops);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Error adding contact", Toast.LENGTH_SHORT).show();
counter--;
}

if (counter > 0) {
if (counter > 1) {
Toast.makeText(this, "Contacts added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Contact added", Toast.LENGTH_SHORT).show();
}
}
}

if (counter == 0) {
Toast.makeText(this, "Unable to add contact", Toast.LENGTH_SHORT).show();
}
}

最佳答案

经过一番折腾后,我发现每次都需要先循环再触发。我不能像上面那样简单地构建 arraylist 并触发一次。以下代码有效:

public void onClick(View v) {
List<AddressContent> contacts = GetAddresses();
int counter = 0;

if (!contacts.isEmpty()) {


for (int i = 0; i < contacts.size(); i++) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,
null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,
null)
.withValue(ContactsContract.RawContacts.AGGREGATION_MODE,
ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED).build());

// ------------------------------------------------------ Name

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
contacts.get(i).getName()).build());

// ------------------------------------------------------ Work
// Number

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
contacts.get(i).getPhone())
.withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.build());

// ------------------------------------------------------ Fax
// Number

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
contacts.get(i).getFax())
.withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK)
.build());

// ------------------------------------------------------ Email

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA,
contacts.get(i).getEmail())
.withValue(
ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());

// ------------------------------------------------------
// Organisation

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Organization.COMPANY,
contacts.get(i).getCompany())
.withValue(
ContactsContract.CommonDataKinds.Organization.TYPE,
ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
.build());

// ------------------------------------------------------
// Address

ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, i)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
contacts.get(i).getStreet())
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
contacts.get(i).getCity())
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
contacts.get(i).getPostcode())
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
contacts.get(i).getCountry())
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
.build());

try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY,
ops);
counter++;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Error adding contact", Toast.LENGTH_SHORT).show();
counter--;
}
}



if (counter > 0) {
if (counter > 1) {
Toast.makeText(this, "Contacts added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Contact added", Toast.LENGTH_SHORT).show();
}
}
}

if (counter == 0) {
Toast.makeText(this, "Unable to add contact", Toast.LENGTH_SHORT).show();
}

关于java - 在 Android 2.3.5 中插入多个联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11886900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com