1. Check out our companion app, Forums for Android! Download from Google Play

How to update native contacts ?

Discussion in 'Android Apps & Games' started by raki_android, Nov 23, 2010.

  1. raki_android

    raki_android New Member
    Thread Starter
    5

    Nov 23, 2010
    2
    0
    5
    Hi Guys,

    I am new to android, so thought of doing a small project on Native Contacts. Application should Add, Update & Delete Native Contacts through my application.

    Adding & Deleting native contacts is working fine, but Update contacts is not working at all.

    This is the code i am using for Add, Update & Delete :-

    Add To Native :-

    ContentResolver cr = getContentResolver();
    Uri uri= People.CONTENT_URI;
    int id = -1;
    ContentValues values = new ContentValues();
    Uri phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
    Uri email_address_Uri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);
    int len = 0;
    Cursor mCursor;
    values = new ContentValues();
    values.put(People.NAME, result[0] + " " +result[1]+ " " + result[2]);
    uri = cr.insert(People.CONTENT_URI, values);
    // cr.update(email_address_Uri, values, res, result);
    values.clear();
    phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
    email_address_Uri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);
    Uri add = Uri.withAppendedPath(uri, People.ContactMethods.DATA);
    mCursor = cr.query(uri, null, null, null, null);
    mCursor.moveToLast();
    id = mCursor.getInt(mCursor.getColumnIndex(People._ID));
    mCursor.close();
    if(result[6].trim().trim().length() > 0){
    values.put(People.Phones.TYPE, People.Phones.TYPE_WORK);
    values.put(People.Phones.NUMBER, result[6].trim());
    cr.insert(phoneUri, values);
    values.clear();
    }
    if(result[7].trim().trim().length() > 0){
    values.put(People.Phones.TYPE, People.Phones.TYPE_HOME);
    values.put(People.Phones.NUMBER, result[7].trim());
    cr.insert(phoneUri, values);
    values.clear();
    }
    if(result[8].trim().length() > 0){
    values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE);
    values.put(People.Phones.NUMBER, result[8].trim());
    cr.insert(phoneUri, values);
    values.clear();
    }

    if(result[4].trim().length() > 0){
    values.put(People.ContactMethods.KIND, Contacts.KIND_POSTAL);
    values.put(People.ContactMethods.DATA, result[4].trim() +"\n" + result[3].trim());
    values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_WORK);
    cr.insert(email_address_Uri, values);
    values.clear();
    }
    if(result[5].trim().length() > 0){
    values.put(People.ContactMethods.KIND, Contacts.KIND_POSTAL);
    values.put(People.ContactMethods.DATA, result[5].trim());
    values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_HOME);
    cr.insert(email_address_Uri, values);
    values.clear();
    }


    Delete :-

    cr.delete(uri, People._ID + "=" + id, null);


    Update :-

    values.put("data1", "Student_Name");
    cr.update(Uri.parse("content://com.android.contacts/data/"),
    values, "raw_contact_id=" + id, null);

    Suppose i am adding a new contact in the Native itself i.e without using with my AddtoNative method :-

    Name : Robert
    Home Phone : 111111111
    Work Phone : 222222222
    Mobile Phone: 333333333
    Work Fax : 444444444
    Home Email : robert@gmail.com

    if i have use update code given below :-

    values.put("data1", "Student_Name");
    cr.update(Uri.parse("content://com.android.contacts/data/"),
    values, "raw_contact_id=" + id, null);


    All data gets updated and gives the result :-

    Name : Student_Name
    Home Phone : Student_Name
    Work Phone : Student_Name
    Mobile Phone: Student_Name
    Work Fax : Student_Name
    Home Email : Student_Name

    But in update, i just want to only update Work phone number or Home Phone number or Home Email field.

    Any suggestions guys.........

    Thanks in Advance.

    Regards
    Raki
     

    Advertisement

  2. sitlet

    213

    Apr 11, 2010
    5,867
    633
    213
    Why would we need an app to edit contacts, when the stock Contacts/People app does this just fine?
     
  3. raki_android

    raki_android New Member
    Thread Starter
    5

    Nov 23, 2010
    2
    0
    5
    i dont know why it has become so complicated to just update the contact. Add & Delete works like a charm...........:mad:
     
  4. sitlet

    213

    Apr 11, 2010
    5,867
    633
    213
    But again, why do we need this app? Every stock android phone can do this already. Just tap a contact, hit Menu, then Edit. It cant get any simplier.
     
  5. rooner

    rooner New Member
    25

    Nov 30, 2010
    4
    0
    25
    I'm not a developer, but I do have the issue of not being able to edit any fields on my contacts other than first and last name. This is a recent bug - I did a reset a couple of weeks ago, and now my corporate contacts (synced from an Exchange account) all show up fine and I can see everything, but if I try to edit a contact, it only shows me the first and last name...
     
  6. sitlet

    213

    Apr 11, 2010
    5,867
    633
    213
    You have to link them to a Google Contact. You cant edit exchange or facebook contacts. create a Google Contact for each contact and link them, then you can edit it.
     
  7. rooner

    rooner New Member
    25

    Nov 30, 2010
    4
    0
    25
    I don't mean to be dense, but how do I go about creating Google contacts for each of my (many) corporate/Exchange contacts? Also, I was perfectly able to edit corporate/Exchange contacts on my Droid until this "bug" surfaced after a system reset...

    Thanks in advance...
     

Share This Page

Loading...