android - How to display numbers taken from contact and Have Uri -
i trying create app selects contact contacts , display name , number on screen. till have created activity select contact , activity returning contacts. successful in showing contact name. have problem in showing numbers in it. there variety of solutions , want simplest one. of them goes through complex methods hard me understand. unable find simple function can display number contactscontract.contacts.display_name
here code!
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_view_contact); intent viewcontact = getintent(); string tempviewcontact = viewcontact.getstringextra(mainactivity.contacturi); uri contacturi = uri.parse(tempviewcontact); cursor cursor; cursor = getcontentresolver().query(contacturi,null,null,null,null); cursor.movetofirst(); int indexname = cursor.getcolumnindex(contactscontract.contacts.display_name); int indexid = cursor.getcolumnindex(contactscontract.contacts._id); //shows contact name string contactname = cursor.getstring(indexname); contact_name = (textview) findviewbyid(r.id.contact_name); contact_name.settext(contactname); contact_num = (textview) findviewbyid(r.id.contact_number); //now ???? }
here small snippet on how collect contacts device
// getting content resolver access contact details contentresolver cr = c.getcontentresolver(); // getting cursor cursor cur1 = cr.query(contactscontract.contacts.content_uri, null, null, null, null); // checking cursor has data if (cur1.getcount() > 0) { // looping through every contact while (cur1.movetonext()) { // getting contact id string id = cur1.getstring(cur1 .getcolumnindex(contactscontract.contacts._id)); // getting contact name string contactname = cur1 .getstring(cur1 .getcolumnindex(contactscontract.contacts.display_name)); string contactnumber = ""; // ensuring number availablity if (integer .parseint(cur1.getstring(cur1 .getcolumnindex(contactscontract.contacts.has_phone_number))) > 0) { // phone numbers stored in table // cursor phone numbers cursor pcur = cr .query(contactscontract.commondatakinds.phone.content_uri, null, contactscontract.commondatakinds.phone.contact_id + " = ?", new string[] { id }, null); // has number in cursor - check + loop while (pcur.movetonext()) { // checking number count single // contact if (pcur.getcount() > 1) { contactnumber = contactnumber + "," + pcur.getstring(pcur .getcolumnindex(contactscontract.commondatakinds.phone.number)); } else { contactnumber = pcur .getstring(pcur .getcolumnindex(contactscontract.commondatakinds.phone.number)); } } } // logging log.d("x", "id:" + id + "\nname:" + contactname + "\nnumber:" + contactnumber); log.i("x", "--------------------------------");
include below permission in manifest
<uses-permission android:name="android.permission.read_contacts" />
if want show name , contact number in list, follow below steps
1) build model class 2 fields name , contact contactmodel
2) make contactmodel list , add each model list.
3) attach data listview using custom adapter.
Comments
Post a Comment