views:

449

answers:

3

I have written an application and added 2 contacts on emulator, but i am not able to update their names on android 2.1, code is working on android 1.6 platform with the following code.

ContentValues contactValues = new ContentValues();
contactValues.put(Contacts.People.NAME, firstName+" "+lastName);
getContentResolver().update(UpdateContactUri, contactValues, null,
null);

In android 1.6 i am getting Uri for those two contacts are "content:// contacts/people/1" and "content://contacts/people/2".

but in 2.1 I am getting these values are "content://contacts/people/8" and "content://contacts/people/9" and while updating its giving "java.IllegalArgumentException, Empty values" exception.

When i tried to put a static Uri like "content://contacts/people/1", code was debugged sucessfully but contact was not updated.

How can i resolve it, why i am not getting uri like 1.6 platform ?

Thanks in advance...

A: 

the 2.1 SDK contains new contentHandler for contacts named ContactsContract the query now moved to look a it different so i'm sure the URI is different too. We work in 2.1 only an able to edit and get contact's fields. see http://developer.android.com/reference/android/provider/ContactsContract.html

eyal
Hi eyal thanks for the reply, now i am able get different uri like content://com.android.contacts/contacts/16 but not able to update name on it by contactValues.putContactsContract.Contacts.DISPLAY_NAME, firstName+" "+lastName);getContentResolver().update(content://com.android.contacts/contacts/16, contactValues, null, null); I am getting 0 as no of rows updated. Is it something wrong here ?
Rishabh
A: 

You can use below code to add contacts in emulator. import android.provider.Contacts.People;

public void addvaluestocontent() { ContentValues values = new ContentValues();

values.put(People.NAME, "Abraham Lincoln");
values.put(People._ID, "1");
values.put(People.NUMBER, "23333");

Uri uri = getContentResolver().insert(People.CONTENT_URI, values);

}

Maneesh
A: 

in android 2.1, I use this hack code to update contact name:

public static void modifyPeopleName(ContentResolver cr, String id,
        String sName) {
    if (sName == null)
        return;

    ContentValues values = new ContentValues();
    int android_sdk_version = Integer.parseInt(Build.VERSION.SDK);
    if (android_sdk_version < 7) {
        values.put(People.NAME, sName);
        cr.update(People.CONTENT_URI, values, People._ID+"="+id, null);
    } else {
        values.put("data1", sName);
        cr.update(Uri.parse("content://com.android.contacts/data/"),
                values, "raw_contact_id=" + id, null);
    }
    return;
}
xufan