contactscontract - Android - contacts address field updating to other fields -
i trying update native contact data through app. problem when updating address, address updating first name , notes , phone fields. below code used in app, please correct me if mistake.
public arraylist<contentprovideroperation> updatecontact(context context, contactdata contactdata){ string selectphone = contactscontract.data.contact_id + "=?"; string[] phoneargs = new string[]{contactdata.getcontactlocalid()}; arraylist <contentprovideroperation> ops = new arraylist <> (); ops.add(contentprovideroperation.newupdate( contactscontract.rawcontacts.content_uri) .withselection(selectphone, phoneargs) .withvalue(contactscontract.rawcontacts.account_type, null) .withvalue(contactscontract.rawcontacts.account_name, null) .build()); //------------------------------------------------------ names ops.add(contentprovideroperation.newupdate( contactscontract.data.content_uri) .withselection(selectphone, phoneargs) .withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.structuredname.content_item_type) .withvalue( contactscontract.commondatakinds.structuredname.given_name, contactdata.getfirstname()) .withvalue( contactscontract.data.mimetype, contactscontract.commondatakinds.structuredname.content_item_type) .withvalue( contactscontract.commondatakinds.structuredname.family_name, contactdata.getlastname()) .build()); //------------------------------------------------------ mobile number ops.add(contentprovideroperation. newupdate(contactscontract.data.content_uri) .withselection(selectphone, phoneargs) .withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.phone.content_item_type) .withvalue(contactscontract.commondatakinds.phone.number, contactdata.getcontactphonebook().get(0).getnumber()) .withvalue(contactscontract.commondatakinds.phone.type, contactscontract.commondatakinds.phone.type_mobile) .build()); //------------------------------------------------------ email ops.add(contentprovideroperation.newupdate(contactscontract.data.content_uri) .withselection(selectphone, phoneargs) .withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.email.content_item_type) .withvalue(contactscontract.commondatakinds.email.data, contactdata.getemailbook().get(0).getemail()) .withvalue(contactscontract.commondatakinds.email.type, contactscontract.commondatakinds.email.type_work) .build()); //------------------------------------------------------ notes ops.add(contentprovideroperation.newupdate(contactscontract.data.content_uri) .withselection(selectphone, phoneargs) .withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.note.content_item_type) .withvalue(contactscontract.commondatakinds.note.note, contactdata.getnotes()) .build()); //------------------------------------------------------ address ops.add(contentprovideroperation.newupdate(contactscontract.data.content_uri) .withselection(selectphone, phoneargs) .withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.structuredpostal.content_item_type) .withvalue(contactscontract.commondatakinds.structuredpostal.street, contactdata.getaddressbook().get(0).getstreet_line_1()) .withvalue(contactscontract.commondatakinds.structuredpostal.type, contactscontract.commondatakinds.structuredpostal.street) .build()); // asking contact provider updating contact try { contentproviderresult[] result = context.getcontentresolver().applybatch(contactscontract.authority, ops); } catch (exception e) { e.printstacktrace(); return null; } return ops; }
finally found answer problem.
string selectphone = contactscontract.data.contact_id + "=? , " + contactscontract.data.mimetype + "= ?"; string[] nameargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.structuredname.content_item_type}; string[] mobileargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.phone.content_item_type}; string[] emailargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.email.content_item_type}; string[] notesargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.note.content_item_type}; string[] addressargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.structuredpostal.content_item_type};
we should pass separate arguments updating.
public contactdata updatecontact(context context, contactdata contactdata){ string selectphone = contactscontract.data.contact_id + "=? , " + contactscontract.data.mimetype + "= ?"; string[] nameargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.structuredname.content_item_type}; string[] mobileargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.phone.content_item_type}; string[] emailargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.email.content_item_type}; string[] notesargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.note.content_item_type}; string[] addressargs = new string[]{contactdata.getcontactlocalid(), contactscontract.commondatakinds.structuredpostal.content_item_type}; arraylist <contentprovideroperation> ops = new arraylist <> (); //------------------------------------------------------ names ops.add(contentprovideroperation.newupdate( contactscontract.data.content_uri) .withselection(selectphone, nameargs) .withvalue(contactscontract.commondatakinds.structuredname.given_name, contactdata.getfirstname()) .withvalue(contactscontract.commondatakinds.structuredname.family_name, contactdata.getlastname()) .build()); //------------------------------------------------------ mobile number ops.add(contentprovideroperation. newupdate(contactscontract.data.content_uri) .withselection(selectphone, mobileargs) .withvalue(contactscontract.commondatakinds.phone.number, contactdata.getcontactphonebook().get(0).getnumber()) .withvalue(contactscontract.commondatakinds.phone.type, contactscontract.commondatakinds.phone.type_mobile) .build()); //------------------------------------------------------ email ops.add(contentprovideroperation.newupdate(contactscontract.data.content_uri) .withselection(selectphone, emailargs) .withvalue(contactscontract.commondatakinds.email.data, contactdata.getemailbook().get(0).getemail()) .withvalue(contactscontract.commondatakinds.email.type, contactscontract.commondatakinds.email.type_work) .build()); //------------------------------------------------------ notes ops.add(contentprovideroperation.newupdate(contactscontract.data.content_uri) .withselection(selectphone, notesargs) .withvalue(contactscontract.commondatakinds.note.note, contactdata.getnotes()) .build()); //------------------------------------------------------ address ops.add(contentprovideroperation.newupdate(contactscontract.data.content_uri) .withselection(selectphone, addressargs) .withvalue(contactscontract.commondatakinds.structuredpostal.street, contactdata.getaddressbook().get(0).getstreet_line_1()) .withvalue(contactscontract.commondatakinds.structuredpostal.type, contactscontract.commondatakinds.structuredpostal.street) .build()); // asking contact provider updating contact try { contentproviderresult[] result = context.getcontentresolver().applybatch(contactscontract.authority, ops); string serverid = markasupdated(contactdata); contactdata.setcontactid(serverid); } catch (exception e) { e.printstacktrace(); return null; } return contactdata; }
Comments
Post a Comment