android - Unable to fetch contact names -


i trying code retrieve contact numbers , contact names. got numbers names list returns null

contentresolver cr = this.getcontentresolver(); //activity/application android.content.context     cursor cursor = cr.query(contactscontract.contacts.content_uri, null, null, null, null);     if(cursor.movetofirst())     {         list<string> contactnames = new arraylist<string>();                 {             string id = cursor.getstring(cursor.getcolumnindex(contactscontract.contacts._id));              if(integer.parseint(cursor.getstring(cursor.getcolumnindex(contactscontract.contacts.has_phone_number))) > 0)             {                 cursor pcur = cr.query(contactscontract.commondatakinds.phone.content_uri,null,contactscontract.commondatakinds.phone.contact_id +" = ?",new string[]{ id }, null);                     while (pcur.movetonext())                 {                     string contactnumber = pcur.getstring(pcur.getcolumnindex(contactscontract.commondatakinds.phone.number));                     string contactnames = pcur.getstring(pcur.getcolumnindex(contactscontract.commondatakinds.phone.display_name));                     contactnames.add(contactnames);                     break;                 }                 pcur.close();             }          } while (cursor.movetonext()) ;     } 

i don't know missing. please help

read contact list phone:

import java.util.arraylist; import android.content.contentresolver; import android.content.context; import android.database.cursor; import android.provider.contactscontract; import android.provider.contactscontract.commondatakinds.email;  public class contactsutils {      public static arraylist<contactmodel> getcontactlist(context ctx) {          arraylist<contactmodel> list = new arraylist<contactmodel>();         string displayname = "", phoneno = "", email = " ";         contentresolver contentresolver = ctx.getcontentresolver();         cursor curmain = contentresolver.query(contactscontract.contacts.content_uri, null, null, null, null);         if (curmain.getcount() > 0) {             while (curmain.movetonext()) {                 string contactid = curmain.getstring(curmain.getcolumnindex(contactscontract.contacts._id));                 displayname = curmain.getstring(curmain.getcolumnindex(contactscontract.contacts.display_name));                 if (integer.parseint(curmain.getstring(curmain.getcolumnindex(contactscontract.contacts.has_phone_number))) > 0) {                      cursor phonecursor = contentresolver.query(contactscontract.commondatakinds.phone.content_uri, null, contactscontract.commondatakinds.phone.contact_id +" = ?",new string[]{contactid}, null);                      contentresolver.query(contactscontract.commondatakinds.phone.content_uri,null,contactscontract.commondatakinds.phone.contact_id+ " = ?", new string[] { contactid }, null);                      cursor emailcursor = contentresolver.query(email.content_uri, null,email.contact_id + " = " + contactid, null, null);                         if(emailcursor.getcount()>0){                               while (emailcursor.movetonext()) {                                   email =   emailcursor.getstring(emailcursor.getcolumnindex(email.data));                             }                         }else{                             email = "";                         }                         emailcursor.close();                         if(phonecursor.getcount()>0){                               while (phonecursor.movetonext()) {                                   phoneno = phonecursor.getstring(phonecursor.getcolumnindex(contactscontract.commondatakinds.phone.number));                               }                         }else{                             phoneno = "";                         }                       phonecursor.close();                      contactmodel cm = new contactmodel();                     cm.setdisplayname(displayname);                     cm.setphonenumber(phoneno);                     cm.setemail(email);                     list.add(cm);                 }             }         }          curmain.close();         return list;     }      public static class contactmodel implements comparable<contactmodel>{         private string displayname = "", phonenumber = "", email = "";            @override         public int compareto(contactmodel another) {             return getdisplayname().compareto(another.getdisplayname());             }           public string getemail() {             return email;         }          public void setemail(string email) {             this.email = email;         }          public string getdisplayname() {             return displayname;         }          public void setdisplayname(string displayname) {             this.displayname = displayname;         }          public string getphonenumber() {             return phonenumber;         }          public void setphonenumber(string phonenumber) {             this.phonenumber = phonenumber;         }           public contactmodel() {         }          public contactmodel(string name) {             this.displayname = name;         }          public string tostring() {             return displayname;         }     } } 

add permission in manifest file:

<uses-permission android:name="android.permission.read_contacts"/> 

hope you.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -