android - How to fetch contact details from Contacts and display it in a textview? -


i want fetch contact details contacts , display in textview. tried below code, application gets crashed. i'm beginner in android , i'm not able identify mistake.can me.. here's code: /mainactivity.java/

package com.example.retrievecontacts;  import android.content.contentresolver; import android.database.cursor; import android.os.bundle; import android.provider.contactscontract; import android.support.v7.app.actionbaractivity; import android.widget.textview;  public class mainactivity extends actionbaractivity {     textview txtv;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         txtv = (textview) findviewbyid(r.id.viewcontacts);         readcontacts();      }      public void readcontacts() {         stringbuffer sb = new stringbuffer();         sb.append("......contact details.....");         contentresolver cr = getcontentresolver();         cursor cur = cr.query(contactscontract.contacts.content_uri, null,                 null, null, null);         string name = null;         string id = null;         string phone = null;         string emailcontact = null;         string emailtype = null;         if (cur.getcount() > 0) {             while (cur.movetonext()) {                 id = cur.getstring(cur                         .getcolumnindex(contactscontract.contacts._id));                 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);                 sb.append("\n contact name:" + name);                 cursor pcur = cr.query(                         contactscontract.commondatakinds.phone.content_uri,                         null, contactscontract.commondatakinds.phone.contact_id                                 + " = ?", new string[] { id }, null);                 while (pcur.movetonext()) {                     phone = pcur                             .getstring(pcur                                     .getcolumnindex(contactscontract.commondatakinds.phone.number));                     sb.append("\n phone number:" + phone);                     system.out.println("phone" + phone);                 }                 pcur.close();                 cursor emailcur = cr.query(                         contactscontract.commondatakinds.email.content_uri,                         null, contactscontract.commondatakinds.email.contact_id                                 + " = ?", new string[] { id }, null);                 while (emailcur.movetonext()) {                     emailcontact = emailcur                             .getstring(emailcur                                     .getcolumnindex(contactscontract.commondatakinds.email.data));                     emailtype = emailcur                             .getstring(emailcur                                     .getcolumnindex(contactscontract.commondatakinds.email.type));                     sb.append("\nemail:" + emailcontact + "email type:"                             + emailtype);                     system.out.println("email " + emailcontact                             + " email type : " + emailtype);                 }                 emailcur.close();             }             sb.append("\n........................................");          }         txtv.settext(sb);     }  }                                /*activity.xml*/  <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.example.retrievecontacts.mainactivity" >  <textview      android:id="@+id/viewcontacts"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignparentleft="true"      android:layout_alignparenttop="true"/>  </relativelayout> 

i wrote permission in manifest as:

then whats problem. why application gets crashed?

you forgot move cursor first position:

cur.movetofirst(); 

your code must like:

 if (cur.getcount() > 0) {             cur.movetofirst();             while (cur.movetonext()) {              .......              ....... 

if not helped post logcat error.

i think must cursorindexoutofboundexception

you need check pcur too,

if (pcur.getcount() > 0) {                 pcur.movetofirst();                 while (pcur.movetonext()) {                   .......                   ....... 

and emailcur , cursor have

one problem have manish mulimani's said in bellow answer, read too

change code with:

if (cur.movetofirst()) {                     {                                             // job                       } while (cur.movetonext()); 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -