Getting phone number from contacts using ContentProvider - Android -


i making small app can contacts of phone using content provider , display them in listview illustrated.

enter image description here

i want select row of listview , automically make phone call specific contact. tried things,but don't work. ideas? here code.

public class mainactivity extends listactivity implements adapterview.onitemclicklistener{ arrayadapter<string> adapter; @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      contentresolver cr = getcontentresolver();     cursor c = cr.query(contactscontract.contacts.content_uri,             new string[] {contactscontract.contacts.display_name},             null, null, null);      list<string> contacts = new arraylist<string>();     if (c.movetofirst()) {         {             contacts.add(c.getstring(c.getcolumnindex(contactscontract.contacts.display_name)));         } while (c.movetonext());     }     adapter = new arrayadapter<string>(this, r.layout.activity_main, contacts);     setlistadapter(adapter);    }  @override public void onitemclick(adapterview<?> parent, view view, int position, long id) {        //the answer should inside here.    } } 

first, ensure have added permission:

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

to androidmanifest.xml file, can loop through phone contacts this:

cursor cursor = getcontentresolver().query(contactscontract.contacts.content_uri,null, null, null, null);  while (cursor.movetonext()) {      string contactid = cursor.getstring(cursor.getcolumnindex(      string hasphone = cursor.getstring(cursor.getcolumnindex(contactscontract.contacts.has_phone_number));      if (boolean.parseboolean(hasphone)) {          // know has number query         cursor phones = getcontentresolver().query( contactscontract.commondatakinds.phone.content_uri, null, contactscontract.commondatakinds.phone.contact_id +" = "+ contactid, null, null);          while (phones.movetonext()) {              string phonenumber = phones.getstring(phones.getcolumnindex( contactscontract.commondatakinds.phone.number));                          }          phones.close();      }      cursor emails = getcontentresolver().query(contactscontract.commondatakinds.email.content_uri, null, contactscontract.commondatakinds.email.contact_id + " = " + contactid, null, null);       while (emails.movetonext()) {          // allow several email addresses          string emailaddress = emails.getstring(          emails.getcolumnindex(contactscontract.commondatakinds.email.data));      }       emails.close(); } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -