android - fetch distinct and unique contacts from the phone avoiding duplicity -
i want retrieve contact list phone avoiding duplicates. think retrieving contacts google account,phone or sim. how avoid duplicate contacts. though contact can have same number different numbers. same name , same number should not appear in list. code.
contentresolver cr = getcontentresolver(); final string[] projection = new string[] { contactscontract.commondatakinds.phone.contact_id, contactscontract.contacts.display_name, contactscontract.commondatakinds.phone.number }; string selection = contactscontract.contacts.in_visible_group + " = '" + ("1") + "'"; //string selection = contactscontract.contacts.has_phone_number; cursor cur = cr.query(contactscontract.commondatakinds.phone.content_uri, projection, selection + " , " + contactscontract.contacts.has_phone_number + "=1", null, contactscontract.commondatakinds.phone.display_name + " collate localized asc"); if(cur != null) { if (cur.getcount() > 0) { while (cur.movetonext()) { string contactphone = cur.getstring(cur.getcolumnindex(contactscontract.commondatakinds.phone.number)); string contactname = cur.getstring(cur.getcolumnindex(contactscontract.commondatakinds.phone.display_name)); string contactid = cur.getstring(cur.getcolumnindex(contactscontract.commondatakinds.phone.contact_id)); contacts.add(new contact().setid(contactid).setname(contactname).setnumber(contactphone)); } } cur.close(); }
i not sure whether reliable or not, worked me
first copy contacts in arraymap
phone numbers key , contact name value. in method, if phone numbers repeated, update name.
once arraymap createt, can use iterator retrieve contact unique phone number.
arraymap<string, string> arraymap = new arraymap<>(); // // while (cursor.movetonext()){ string phone = cursor.getstring(cursor.getcolumnindex(contactscontract.commondatakinds.phone.number)); string name = cursor.getstring(cursor.getcolumnindex(contactscontract.commondatakinds.phone.display_name)); arraymap.put(phone, name); } // // for(string key: arraymap.keyset()){ jsonobject jsonobject = new jsonobject(); try { jsonobject.put("name", arraymap.get(key)); jsonobject.put("phone_number", key); contactslist.put(jsonobject); }catch(jsonexception e){ e.printstacktrace(); } }
in case can directly call new contacts(...)
instead of using jsonobject.
Comments
Post a Comment