title: update contacts display_name
body: <p>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.</p>
  
  <p>Here is the code.  Early on, it fetches an id from the aggregate contacts as follows, where <em>key</em> is the lookup_key:</p>
  
  <pre><code>  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();
  </code></pre>
  
  <p>Then, after the user has done his edits, I call this block of code to update the display_name:</p>
  
  <pre><code>  ArrayList&lt;ContentProviderOperation&gt; opers = new ArrayList&lt;ContentProviderOperation&gt;();
    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 ...
  </code></pre>
  
  <p>I realize I don't need the ContentProviderOperation for this example; that's for later when I have more stuff to update.</p>
  
  <p>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.</p>
  
  <p>For what it's worth, looking at <em>results</em> 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.  </p>