在Android 2.3.5中插入多个联系人

时间:2012-08-09 15:36:50

标签: java android

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

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

我的点击事件启动了获取数据然后将其添加到联系人的命令。我可以确认每次都将数据读入联系人列表,因此数据不存在问题。

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();
    }
}

2 个答案:

答案 0 :(得分:2)

经过多次搞乱后,我发现每次都需要循环然后开火。如上所述,我不能简单地建立一个arraylist和fire。以下代码有效:

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();
}

}

答案 1 :(得分:0)

以上答案不正确您可以一次插入多个联系人,只要您设置正确的反向引用ID,如下所示:

    int backReference;
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    for (int i = 0; i < contacts.size(); i++)  {
        Contact contact = contacts.get(i);
        backReference = ops.size();
        ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(RawContacts.CONTENT_URI, true))
                .withYieldAllowed(true)
                .withValue(RawContacts.ACCOUNT_NAME, contact.fullName)
                .withValue(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE)
                .withValue(RawContacts.SOURCE_ID, contact.contactId)
                .build());

        ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Settings.CONTENT_URI, true))
                .withValue(RawContacts.ACCOUNT_NAME, contact.fullName)
                .withValue(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE)
                .withValue(ContactsContract.Settings.UNGROUPED_VISIBLE, 1)
                .build());

        ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
                .withValueBackReference(Data.RAW_CONTACT_ID, backReference)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.GIVEN_NAME, contact.firstName)
                .withValue(StructuredName.DISPLAY_NAME, contact.fullName)
                .withValue(StructuredName.FAMILY_NAME, ((contact.preposition != null && !contact.preposition.equals("")) ? contact.preposition + " " : "") + contact.lastName)
                .build());


        for (Number number : contact.numbers) {
            ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
                    .withValueBackReference(Data.RAW_CONTACT_ID, backReference)
                    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
                    .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number.number)
                    .build());
        }

        for (Email email : contact.emails) {
            ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
                    .withValueBackReference(Data.RAW_CONTACT_ID, backReference)
                    .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
                    .withValue(Email.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
                    .withValue(Email.DATA, email.email)
                    .withValue(Email.LABEL, TYPE)
                    .build());
        }
        if (contacts.size() > i+1 && ops.size() + contacts.get(i+1).getTotalInsertions() > 499) {
            try {
                resolver.applyBatch(ContactsContract.AUTHORITY, ops);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            ops.clear();
        } else if (contacts.size() == i+1) {
            try {
                resolver.applyBatch(ContactsContract.AUTHORITY, ops);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }