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 8943245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:43:39+00:00 2026-06-15T11:43:39+00:00

How do I update the display name for a contact? The operation in the

  • 0

How do I update the display name for a contact? The operation in the code below completes without throwing anything and appears to work – that is, when I requeried the ContactsContract.Contact table, a row came back with the name changed. However, when I tried running the stock “people” app on my tablet, it crashed. Evidentally I did something wrong.

Here is the code. Early on, it fetches an id from the aggregate contacts as follows, where key is the lookup_key:

  String[] projection = new String[] {
    Contacts._ID, // 0
    Contacts.DISPLAY_NAME, // 1
  };

  Uri uri = Uri.parse (Contacts.CONTENT_LOOKUP_URI + "/" + key);
  ContentResolver cr = getContentResolver();
  Cursor cursor = cr.query (uri, projection, null, null, null);
  if (!cursor.moveToNext()) // move to first (and only) row.
    throw new IllegalStateException ("contact no longer exists for key");
  origId = cursor.getLong(0);
  cursor.close();

Then, after the user has done his edits, I call this block of code to update the display_name:

  ArrayList<ContentProviderOperation> opers = new ArrayList<ContentProviderOperation>();
  ContentProviderOperation.Builder builder = null;

  String[] args = { Long.toString (origId) };
  builder = ContentProviderOperation.newUpdate (Data.CONTENT_URI);
  builder.withSelection (RawContacts.CONTACT_ID + "=?", args);
  builder.withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, name);
  opers.add(builder.build());

  ContentProviderResult[] results = null;
  try {
    results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, opers);
  } catch ...

I realize I don’t need the ContentProviderOperation for this example; that’s for later when I have more stuff to update.

To be honest, I’m pretty confused about which ID I’m actually using. The names aren’t that clear to me and I may be using the wrong ID for this operation.

For what it’s worth, looking at results after the update I saw a result code of 5. I can’t find any documentation for that, so have no idea if that is significant.

  • 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-15T11:43:40+00:00Added an answer on June 15, 2026 at 11:43 am

    The IDs (and altering contacts in general) can be pretty confusing… I had some dramas getting my head around them as well.

    Here is some working code I use for updating. The main difference I can see is how you are declaring the raw ID; it needs to be included in as a content value.

    Cursor cursor = _context.getContentResolver().query(contactUri,
                new String[] { Contacts._ID }, null, null, null);
        try {
            if (cursor.moveToFirst()) {
                String rawContactId = cursor.getString(0);
                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
                ContentValues contentValues = new ContentValues();
                contentValues.put(Data.RAW_CONTACT_ID, rawContactId);
    
                contentValues
                        .put(ContactsContract.Data.MIMETYPE,
                                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                contentValues.put(
                        ContactsContract.CommonDataKinds.Phone.NUMBER,
                        phoneNumber);
                contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
    
                ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValues(contentValues).build());
                String contactId = contactUri.getLastPathSegment();
                ops.add(ContentProviderOperation
                            .newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(
                                    ContactsContract.Data.CONTACT_ID
                                            + "=? AND "
                                            + ContactsContract.Data.MIMETYPE
                                            + "=?",
                                    new String[] {
                                            contactId,
                                            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE })
                            .withValue(
                                    ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                                    newName).build());
    
                result = _context.getContentResolver().applyBatch(
                        ContactsContract.AUTHORITY, ops);
            }
        } finally {
            cursor.close();
        }
    

    hopefully it helps!

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

Sidebar

Related Questions

I've created a custom module that users can update their first name, last name
I'm making a list that display Phone Contact. It works but does not looks
I'm trying to update the display brightness from a widget but i have some
Because of min time betwwen update it display the location update after 1 sec
I am trying to get my first button to update a display number in
I'd like to split apart a DateTime for both display and update within a
i need to update my application so it can support retina display in New
The following script will update the database, but it won't display the correct page
I'm trying to insert and update a piece of information on an existing contact
Have a simple contact us XPage created. Have server side validation in place that

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.