Few (specific everytime) contacts duplicated during insert in android -
i trying insert list of contacts in android using contact api. facing strange problem that. of contact getting duplicated if exist in phone book. happens few of contacts not contacts confused me more.
here code.
final arraylist<contentprovideroperation> restoredcontactlist = new arraylist<contentprovideroperation>(); restoredcontactlist.clear(); restoredcontactlist .add(contentprovideroperation .newinsert( contactscontract.rawcontacts.content_uri) .withvalue( contactscontract.rawcontacts.account_type, maccounttype) .withvalue( contactscontract.rawcontacts.account_name, maccountname).build()); (int j = 0; j < namesarray.length(); j++) { final jsonobject nameobj = namesarray .getjsonobject(j); restoredcontactlist .add(contentprovideroperation .newinsert( contactscontract.data.content_uri) .withvaluebackreference( contactscontract.data.raw_contact_id, 0) .withvalue( contactscontract.data.mimetype, contactscontract.commondatakinds.structuredname.content_item_type) .withvalue( contactscontract.commondatakinds.structuredname.display_name, nameobj.getstring("first_name") + " " + nameobj .getstring("last_name")) .withvalue( contactscontract.commondatakinds.structuredname.given_name, nameobj.getstring("first_name")) .withvalue( contactscontract.commondatakinds.structuredname.family_name, nameobj.getstring("last_name")) .build()); } // phone final jsonarray phonesarray = obj .getjsonarray("phones"); (int j = 0; j < phonesarray.length(); j++) { final jsonobject phoneobj = phonesarray .getjsonobject(j); restoredcontactlist .add(contentprovideroperation .newinsert( contactscontract.data.content_uri) .withvaluebackreference( contactscontract.data.raw_contact_id, 0) .withvalue( contactscontract.data.mimetype, contactscontract.commondatakinds.phone.content_item_type) .withvalue( contactscontract.commondatakinds.phone.number, phoneobj.getstring("phone_number")) .withvalue( contactscontract.commondatakinds.phone.type, gettypeofnumber(phoneobj .getstring("phone_type"))) .build()); } // email final jsonarray emailsarray = obj .getjsonarray("emails"); (int j = 0; j < emailsarray.length(); j++) { final jsonobject mailobj = emailsarray .getjsonobject(j); restoredcontactlist .add(contentprovideroperation .newinsert( contactscontract.data.content_uri) .withvaluebackreference( contactscontract.data.raw_contact_id, 0) .withvalue( contactscontract.data.mimetype, contactscontract.commondatakinds.email.content_item_type) .withvalue( contactscontract.commondatakinds.email.data, mailobj.getstring("email_address")) .withvalue( contactscontract.commondatakinds.email.type, gettypeofemail(mailobj .getstring("email_type"))) .build()); } // address final jsonarray addressesarray = obj .getjsonarray("address"); (int j = 0; j < addressesarray.length(); j++) { final jsonobject addressobj = addressesarray .getjsonobject(j); restoredcontactlist .add(contentprovideroperation .newinsert( contactscontract.data.content_uri) .withvaluebackreference( contactscontract.data.raw_contact_id, 0) .withvalue( contactscontract.data.mimetype, contactscontract.commondatakinds.structuredpostal.content_item_type) .withvalue( contactscontract.commondatakinds.structuredpostal.street, addressobj .getstring("street")) .withvalue( contactscontract.commondatakinds.structuredpostal.city, addressobj .getstring("city")) .withvalue( contactscontract.commondatakinds.structuredpostal.region, addressobj .getstring("state")) .withvalue( contactscontract.commondatakinds.structuredpostal.postcode, addressobj .getstring("zipcode")) .withvalue( contactscontract.commondatakinds.structuredpostal.country, addressobj .getstring("country")) .withvalue( contactscontract.commondatakinds.structuredpostal.type, gettypeofaddress(addressobj .getstring("addresstype"))) .build()); } // asking contact provider create new // contact getcontentresolver().applybatch( contactscontract.authority, restoredcontactlist);
i having 128 contacts in device already.
basically app restore contacts server. first time when restored contact @ time deleted existing contact phone book, works fine. in phone having 128 contacts , trying restore contacts again,but got 133 contacts, 5 of contacts duplicated in case. don't know why happening.
i not getting error or exception. think making small mistake,but not able recognize it. if has idea,please kindly me.
Comments
Post a Comment