Your update seems to be written on the assumption that there's a single row in ContactsContract.Data which has both the structured name fields and the phone number, and which is identified by the contact ID. Unfortunately, none of that is true:
First off, the name and the phone number will generally be in separate ContactsContract.Data rows. (If there is only one phone number; there may be none, or there may be more than one, in which case there will be one ContactsContract.Data row for each, with the mime type set to the value of ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE to distinguish it from rows containing, say, email addresses, postal addresses, or ... well, names.) Second, you can't set the Contact ID for a ContactsContract.Data row directly. You can, instead, set the "Raw Contact ID". "Raw Contacts" are contact information associated with a given account. If you have only one account on the phone (or none), these will almost always be associated with distinct Contacts. However, if you have more than one account (say, a personal Gmail account and an office exchange account), it's possible that you'll have an entry for the same person in each account. In that case, the contacts ContentProvider will represent the two as separate RawContacts, but synthesize a single aggregated Contact that includes data from both. (How does it know? By similarity of names, common email addresses, etc. --- which is not exactly foolproof!) Some stuff that might be helpful: A general overview of the data model (from back when this API was first introduced; the earlier, simpler API it mentions is deprecated and only partially functional on most modern devices --- it'll break if there's more than one account): http://developer.android.com/resources/articles/contacts.html A sample app which inserts a new contact: http://developer.android.com/resources/samples/ContactManager/index.html You might also want to review the Javadoc for ContactsContract and its components, particularly the recommendations for batch updates, etc. Good luck... rst On Sat, Feb 11, 2012 at 1:16 AM, darrinps <[email protected]> wrote: > I am having...mmm....difficulty let's say, updating the contact list. > I can add to it just fine, but when I go to update it, it processes, > but makes no change. Here is my code: > > private void continueHandleEdit() > { > ArrayList<ContentProviderOperation> ops = new > ArrayList<ContentProviderOperation>(); > > String displayName = firstNameEdit.getText().toString() + " " > + lastNameEdit.getText().toString(); > String phoneNumber = phoneNumberEdit.getText().toString(); > > > > ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) > .withSelection(Data._ID + "=?", new String[] > {mContactId}) > .withValue(StructuredName.DISPLAY_NAME, displayName) > .withValue(Phone.NUMBER, phoneNumber) > .build()); > > try > { > Log.d(TAG, "Applying: " + ops); > > ContentProviderResult[] res = > getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); > > if (res!=null && res[0]!=null) > { > int updated = res[0].count; > > > Log.d(TAG, "Updated " + updated + " row(s)"); > } > else > { > Log.e(TAG, "Contact not updated."); > } > > } > catch (RemoteException e) > { > Log.e(TAG, "RemoteEception caught: " + e.getMessage() + > "\nCause: " + e.getCause()); > } > catch (OperationApplicationException e) > { > Log.e(TAG, "OperationApplicationEception caught: " + > e.getMessage() + "\nCause: " + e.getCause()); > } > > finish(); > } > > Now the funny thing is that the log is showing the following: > > [mType: 2, mUri: content://com.android.contacts/data, mSelection: > _id=?, mExpectedCount: null, mYieldAllowed: false, mValues: > data1=5446879999, mValuesBackReferences: null, > mSelectionArgsBackReferences: null] > > Updated 1 row(s) > > I know that the mContactId value holds the ID of the row as I verified > it. Also notice that the phone number is in there (5446879999) but > where is the display name? I know the variable held the right data as > I stepped through it and verified that. > > What am I doing wrong? > > It shouldn't be this much of a pain to update a contact! Grrrrrr. > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

