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

  • SEARCH
  • Home
  • 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 7726461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:13:17+00:00 2026-06-01T05:13:17+00:00

I am currently working on an app, where I need to insert contacts into

  • 0

I am currently working on an app, where I need to insert contacts into the mobilephones contact list. Using the ContactsContract.RawContacts batch insertion, I successfully inserted contacts into the phonebook.

http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html

However when trying to update them they just add more phonenumber, instead replacing the old one.

So I came onto ContactsContract.Data where through the Contact_ID one could apparently on update replace the old phonenumber.

http://developer.android.com/reference/android/provider/ContactsContract.Data.html

My question now is, is it possible to retrieve the Contacts_ID on insertion? So that when I insert a contact by this code:

ArrayList<ContentProviderOperation> ops =
          new ArrayList<ContentProviderOperation>();
 ...
 int rawContactInsertIndex = ops.size();
 ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
          .withValue(RawContacts.ACCOUNT_TYPE, accountType)
          .withValue(RawContacts.ACCOUNT_NAME, accountName)
          .build());

 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
          .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
          .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
          .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
          .build());

 getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

I would get back the Contacts_ID?

Thank you advance for any help you can give me.

  • 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-01T05:13:18+00:00Added an answer on June 1, 2026 at 5:13 am

    Better way, and you get the _ID not the RAW_ID!!

    Add that as static variables:

    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.Contacts.STARRED,
        ContactsContract.Contacts.TIMES_CONTACTED,
        ContactsContract.Contacts.CONTACT_PRESENCE,
        ContactsContract.Contacts.PHOTO_ID,
        ContactsContract.Contacts.LOOKUP_KEY,
        ContactsContract.Contacts.HAS_PHONE_NUMBER,
    };
    

    Then add that after the insert:

    // Displayed name
    String displayed_name = "Contact's name goes here";
    
    // Add the new contact
    try
    {
        context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    
    String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +displayed_name+ "\" )";
    Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    context.startManagingCursor(c);
    
    if (c.moveToNext())
    {
        int id = new Integer(c.getString(0)).intValue();
    }
    

    It works for me.

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

Sidebar

Related Questions

I'm currently working on a Silverlight app and need to convert XML data into
I'm currently working on an app that is being created using Ironspeed Designer. It's
I am currently working on a web app using Wicket and started using jQuery
I'm currently working on a heavy app, and I need the same kind of
I am currently working on creating an application similar to the Contacts app on
I'm currently working on a project using Flask and Google App Engine . Calling
I am currently working on my first android app. In a listview, I need
i'm currently working in a web app and i'm using django-jquery, and when i
I'm currently working on an app that allows the user to draw pixelated images
I'm currently working on an app for a customer of mine and it will

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.