listview - Show contacts in android -
i using code display contacts in lisview. working fine except 1 thing. it's showing contacts including email id , other contacts.
i want show contacts whoever has phone numbers. how can that?
here code:-
public class phonebookactivity extends activity { //android listview object listview listviewphonebook; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.phone_book); //get listview reference xml file listviewphonebook=(listview)findviewbyid(r.id.listphonebook); //arraycolumns array contain contacts name in cursor, cursor data contacts database. //here displaying name contacts database string[] arraycolumns = new string[]{contactscontract.contacts.display_name}; //arrayviewid id of view map here textviewname , can add more views per requirement int[] arrayviewid = new int[]{r.id.textviewname}; //reference phone contacts database using cursor , uri in android. cursor cursor; cursor = getcontentresolver().query(contactscontract.contacts.content_uri, null, null, null, null); /*create adapter arguments layoutid, cursor, array of columns, , array of views populated adapter associated listview populate items directly. adapter associated each_contact.xlm file view in activity */ simplecursoradapter adapter = new simplecursoradapter(this, r.layout.each_contact, cursor, arraycolumns, arrayviewid); listviewphonebook.setadapter(adapter); /*this code click event of item in list view. when click on na contact's name in list view. give item name , position in listview. note that: if want query contacts name details(like phone number clicked contact name & details, need query again. explain in separate post in blog.*/ // handle click on list view item listviewphonebook.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> arg0, view v,int position, long arg3) { // position parameter gives index or position of listview item clicked textview tv=(textview)v.findviewbyid(r.id.textviewname); string name=tv.gettext().tostring(); toast.maketext(getapplicationcontext(), "contact selected: "+name, toast.length_long).show(); } }); } } update:---
i solved 1 of problem myself. have issue. how can phone number on phone contact click in list view?
i trying this:
// handle click on list view item listviewphonebook.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> arg0, view v,int position, long arg3) { // position parameter gives index or position of listview item clicked textview tv=(textview)v.findviewbyid(r.id.textviewname); string name=tv.gettext().tostring(); toast.maketext(getapplicationcontext(), "contact selected: " + position, toast.length_long).show(); cursor c = (cursor) arg0.getitematposition(position); // cursor c = (cursor)arg0.getadapter().getitem(position); string number = c.getstring(c.getcolumnindex(contactscontract.commondatakinds.phone.number)); toast.maketext(getapplicationcontext(), "contact selected: " + number, toast.length_long).show(); } }); error:- java.lang.illegalstateexception: couldn't read row 0, col -1 cursorwindow. make sure cursor initialized correctly before accessing data it. @ android.database.cursorwindow.nativegetstring(native method)
you can read of telephone numbers associated contact in following manner:
uri personuri = contenturis.withappendedid(people.content_uri, personid); uri phonesuri = uri.withappendedpath(personuri, people.phones.content_directory); string[] proj = new string[] {phones._id, phones.type, phones.number, phones.label} cursor cursor = contentresolver.query(phonesuri, proj, null, null, null);
Comments
Post a Comment