Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8377531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:41:41+00:00 2026-06-09T15:41:41+00:00

I am trying to add multiple contacts from my application. I am reading in

  • 0

I am trying to add multiple contacts from my application. I am reading in data from an xml file and then wrapping this up into a list of strongly typed objects. These objects and then looped through and I am calling an insert into the users contacts for each one.

However I am getting some very bizarre results whilst testing where I am getting either 1 or two working contacts and then a number of empty (Unknown) entries. The number of successful entries over (Unknowns) appears to be random but I am at a loss as to why I cannot perform something that should be fairly simple to do.

My click event which initiates the command to fetch the data and then add it to the contacts is below. I can confirm that the data is being read into the contacts list each time so it is not a problem with the data.

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:41:42+00:00Added an answer on June 9, 2026 at 3:41 pm

    After much messing around I have found that I need to loop then fire each time. I cannot simply build up the arraylist and fire once as above. The following code works:

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

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add my data into multiple columns ListBox, I did it
I'm trying to add multiple jQuery data entries to a single element. I suspected
I'm trying to add multiple categories to a Wordpress (3.3.1) post via XMLRPC. This
i was trying to add multiple to address like this. MailAddress mailAddressTo = new
How can I add multiple implementations to this header file: MoveAgent.h #ifndef _GAMEAGENT_ #define
I've been trying to add multiple textviews and buttons when onClick, and this is
I am getting this error when trying to add multiple properties. public NewPropertyHelper(DataLayer.IAccrualRepository Repository)
I'm trying to add a multiple select list to my backend component, but I
I am trying to add multiple google markers on a map and I am
I am trying to add multiple markers each with its own infowindow that comes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.