Getting android contacts -


im trying list of contacts , there email address.

currently im using code, lot of duplicate names , emails. want 1 person , 1 email. there way can merge contacts while querying them?

also im looking > 2.0 solution.

    private void init() {      contentresolver cr = getcontentresolver();     cursor cur = cr.query(contactscontract.contacts.content_uri,null, null, null, null);     if (cur.getcount() > 0) {     while (cur.movetonext()) {         string id = cur.getstring(cur.getcolumnindex(contactscontract.contacts._id));     string name = cur.getstring(cur.getcolumnindex(contactscontract.contacts.display_name));         cursor emailcur = cr.query(contactscontract.commondatakinds.email.content_uri,null,contactscontract.commondatakinds.email.contact_id + " = ?", new string[]{id},null);          while (emailcur.movetonext()) {                  string email = emailcur.getstring( emailcur.getcolumnindex(contactscontract.commondatakinds.email.data));                 log.e("email",name+" "+email);             }              emailcur.close();         }      } 

try out below working code. when working please accept answers.

public class dddddddddd extends activity {     /** called when activity first created. */     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);        // setcontentview(r.layout.main);         readcontacts();     }      public void readcontacts(){          contentresolver cr = getcontentresolver();          cursor cur = cr.query(contactscontract.contacts.content_uri,null, null, null, null);           if (cur.getcount() > 0) {             while (cur.movetonext()) {                 string id = cur.getstring(cur.getcolumnindex(contactscontract.contacts._id));                 string name = cur.getstring(cur.getcolumnindex(contactscontract.contacts.display_name));                 if (integer.parseint(cur.getstring(cur.getcolumnindex(contactscontract.contacts.has_phone_number))) > 0) {                     system.out.println("name : " + name + ", id : " + id);                      // phone number                     cursor pcur = cr.query(contactscontract.commondatakinds.phone.content_uri,null,                                            contactscontract.commondatakinds.phone.contact_id +" = ?",                                            new string[]{id}, null);                     while (pcur.movetonext()) {                           string phone = pcur.getstring(                                  pcur.getcolumnindex(contactscontract.commondatakinds.phone.number));                           system.out.println("phone" + phone);                     }                     pcur.close();                       // email , type                      cursor emailcur = cr.query(contactscontract.commondatakinds.email.content_uri,                             null,                             contactscontract.commondatakinds.email.contact_id + " = ?",                             new string[]{id}, null);                     while (emailcur.movetonext()) {                         // allow several email addresses                             // if email addresses stored in array                         string email = emailcur.getstring(                                       emailcur.getcolumnindex(contactscontract.commondatakinds.email.data));                         string emailtype = emailcur.getstring(emailcur.getcolumnindex(contactscontract.commondatakinds.email.type));                          int type = emailcur.getint(emailcur.getcolumnindex(contactscontract.commondatakinds.email.type));                         string customlabel = emailcur.getstring(emailcur.getcolumnindex(contactscontract.commondatakinds.email.label));                         charsequence customemailtype = contactscontract.commondatakinds.email.gettypelabel(this.getresources(), type, customlabel);                        //system.out.println("email " + email + " email type : " + emailtype);                     }                     emailcur.close();                      // note.......                     string notewhere = contactscontract.data.contact_id + " = ? , " +                             contactscontract.data.mimetype + " = ?";                     string[] notewhereparams = new string[]{id,                     contactscontract.commondatakinds.note.content_item_type};                       cursor notecur = cr.query(contactscontract.data.content_uri, null,                               notewhere, notewhereparams, null);                     if (notecur.movetofirst()) {                         string note = notecur.getstring(                         notecur.getcolumnindex(contactscontract.commondatakinds.note.note));                       system.out.println("note " + note);                     }                     notecur.close();                      //get postal address....                      string addrwhere = contactscontract.data.contact_id                              + " = ? , " + contactscontract.data.mimetype + " = ?";                     string[] addrwhereparams = new string[]{id,                         contactscontract.commondatakinds.structuredpostal.content_item_type};                     cursor addrcur = cr.query(contactscontract.data.content_uri,                                 null, null, null, null);                     while(addrcur.movetonext()) {                         string pobox = addrcur.getstring(                                      addrcur.getcolumnindex(contactscontract.commondatakinds.structuredpostal.pobox));                         string street = addrcur.getstring(                                      addrcur.getcolumnindex(contactscontract.commondatakinds.structuredpostal.street));                         string city = addrcur.getstring(                                      addrcur.getcolumnindex(contactscontract.commondatakinds.structuredpostal.city));                         string state = addrcur.getstring(                                      addrcur.getcolumnindex(contactscontract.commondatakinds.structuredpostal.region));                         string postalcode = addrcur.getstring(                                      addrcur.getcolumnindex(contactscontract.commondatakinds.structuredpostal.postcode));                         string country = addrcur.getstring(                                      addrcur.getcolumnindex(contactscontract.commondatakinds.structuredpostal.country));                         string type = addrcur.getstring(                                      addrcur.getcolumnindex(contactscontract.commondatakinds.structuredpostal.type));                          // these....                      }                     addrcur.close();                      // instant messenger.........                     string imwhere = contactscontract.data.contact_id + " = ? , "                      + contactscontract.data.mimetype + " = ?";                     string[] imwhereparams = new string[]{id,                         contactscontract.commondatakinds.im.content_item_type};                     cursor imcur = cr.query(contactscontract.data.content_uri,                             null, imwhere, imwhereparams, null);                     if (imcur.movetofirst()) {                         string imname = imcur.getstring(                                  imcur.getcolumnindex(contactscontract.commondatakinds.im.data));                         string imtype;                         imtype = imcur.getstring(                                  imcur.getcolumnindex(contactscontract.commondatakinds.im.type));                     }                     imcur.close();                      // organizations.........                      string orgwhere = contactscontract.data.contact_id + " = ? , " + contactscontract.data.mimetype + " = ?";                     string[] orgwhereparams = new string[]{id,                         contactscontract.commondatakinds.organization.content_item_type};                     cursor orgcur = cr.query(contactscontract.data.content_uri,                                 null, orgwhere, orgwhereparams, null);                     if (orgcur.movetofirst()) {                         string orgname = orgcur.getstring(orgcur.getcolumnindex(contactscontract.commondatakinds.organization.data));                         string title = orgcur.getstring(orgcur.getcolumnindex(contactscontract.commondatakinds.organization.title));                     }                     orgcur.close();                   }             }        }     }  } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -