Displaying the phone number of a contact in Android Contact Content Provider -
i trying write program display id, name , phone number of contact in android. far id , names of contact displayed number of contact doesn't. can me, below code program:
package com.example.oghenekaroedoh.provider; import android.app.listactivity; import android.content.cursorloader; import android.database.cursor; import android.net.uri; import android.os.bundle; import android.provider.contactscontract; import android.util.log; import android.widget.cursoradapter; import android.widget.simplecursoradapter; public class provider2activity extends listactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_provider); uri allcontacts = contactscontract.contacts.content_uri; //query string our contacts //uri allcontacts = uri.parse("content://contacts/people"); //declare our cursor cursor c; string[] projection = new string[] {contactscontract.contacts._id, contactscontract.contacts.display_name, contactscontract.contacts.has_phone_number}; //---detect android version //---projection, filtering, , sorting /* >>the second parameter of managedquery() method (third parameter cursorloader class) controls how many columns returned query; parameter known projection >>the third parameter of managedquery() method (fourth parameter cursorloader class) enable specify sql clause filter result of query >>the fourth parameter of managedquery() method (the fifth parameter cursorloader class) enables specify sql order clause sort result of query, either in ascending or descending order * */ if (android.os.build.version.sdk_int <11) { //---if device ids running on os before honeycomb //use managedquery() of activity class retrieve managed cursor c = managedquery(allcontacts, projection, null, null, null); } else { //---honeycomb , later use cursor loader class retrieve managed cursor--- cursorloader cursorloader = new cursorloader( this, allcontacts, projection, null, null , null); c = cursorloader.loadinbackground(); } //create columns contacts display name , column string[] columns = new string[] { contactscontract.contacts.display_name, contactscontract.contacts._id}; int[] views = new int[] {r.id.contactname, r.id.contactid}; simplecursoradapter adapter; //detect android version again..s if (android.os.build.version.sdk_int <11) { //---if before honeycomb--- //use simplecursoradapter class map cursor view (like textviews imageviews e.t.c) adapter = new simplecursoradapter( this, r.layout.activity_provider, c, columns, views); } else { //---honeycomb , later--- ////use simplecursoradapter class map cursor view (like textviews imageviews e.t.c) //with parameter known cursoradapter.flag_register_current_observer adapter = new simplecursoradapter( this, r.layout.activity_provider, c, columns, views, cursoradapter.flag_register_content_observer); } this.setlistadapter(adapter); printcontacts(c); } private void printcontacts(cursor c) { //---display contact id , name , phone number---- if (c.movetofirst()) { do{ //---get contact id , name string contactid = c.getstring(c.getcolumnindex( contactscontract.contacts._id)); string contactdisplayname = c.getstring(c.getcolumnindex( contactscontract.contacts.display_name)); log.v("content providers", contactid + ", " + contactdisplayname); //---get phone number--- int hasphone = c.getint(c.getcolumnindex( contactscontract.contacts.has_phone_number)); if (hasphone == 1) { cursor phonecursor = getcontentresolver().query( contactscontract.commondatakinds.phone.content_uri, null, contactscontract.commondatakinds.phone.contact_id + " = " + contactid, null, null); while (phonecursor.movetonext()) { log.v("content providers", phonecursor.getstring( phonecursor.getcolumnindex( contactscontract.commondatakinds.phone.number))); } phonecursor.close(); } } while (c.movetonext()); } } }
i got example working adding customadapter myclassadapter
extends arrayadapter
, , populating arraylist
of contact
objects.
import android.app.listactivity; import android.content.contentresolver; import android.content.context; import android.content.cursorloader; import android.database.cursor; import android.net.uri; import android.os.bundle; import android.provider.contactscontract; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; import java.util.arraylist; public class provider2activity extends listactivity { arraylist<contact> contacts; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_provider2); contacts = new arraylist<contact>(); uri allcontacts = contactscontract.contacts.content_uri; //declare our cursor cursor c; string[] projection = new string[] {contactscontract.contacts._id, contactscontract.contacts.display_name, contactscontract.contacts.has_phone_number}; /* >>the second parameter of managedquery() method (third parameter cursorloader class) controls how many columns returned query; parameter known projection >>the third parameter of managedquery() method (fourth parameter cursorloader class) enable specify sql clause filter result of query >>the fourth parameter of managedquery() method (the fifth parameter cursorloader class) enables specify sql order clause sort result of query, either in ascending or descending order * */ if (android.os.build.version.sdk_int <11) { //---if device ids running on os before honeycomb //use managedquery() of activity class retrieve managed cursor c = managedquery(allcontacts, projection, null, null, null); } else { //---honeycomb , later use cursor loader class retrieve managed cursor--- cursorloader cursorloader = new cursorloader( this, allcontacts, projection, null, null , null); c = cursorloader.loadinbackground(); } printcontacts(c); myclassadapter adapter; //detect android version again.. if (android.os.build.version.sdk_int <11) { //---if before honeycomb--- adapter = new myclassadapter( this, r.layout.row_line, contacts); } else { //---honeycomb , later--- adapter = new myclassadapter( this, r.layout.row_line, contacts); } this.setlistadapter(adapter); } private void printcontacts(cursor c) { contentresolver cr = getcontentresolver(); //---display contact id , name , phone number---- if (c.movetofirst()) { do{ //---get contact id , name string contactid = c.getstring(c.getcolumnindex( contactscontract.contacts._id)); string contactdisplayname = c.getstring(c.getcolumnindex( contactscontract.contacts.display_name)); log.v("content providers", contactid + ", " + contactdisplayname); string contactdisplayphone = ""; //---get phone number--- int hasphone = c.getint(c.getcolumnindex( contactscontract.contacts.has_phone_number)); if (hasphone == 1) { cursor phonecursor = getcontentresolver().query( contactscontract.commondatakinds.phone.content_uri, null, contactscontract.commondatakinds.phone.contact_id + " = " + contactid, null, null); while (phonecursor.movetonext()) { log.v("content providers", contactdisplayphone = phonecursor.getstring( phonecursor.getcolumnindex( contactscontract.commondatakinds.phone.number))); } phonecursor.close(); } contacts.add(new contact(contactdisplayname, contactid, contactdisplayphone)); } while (c.movetonext()); } } public class contact{ public string contactname = ""; public string contactid = ""; public string contactnumber = ""; public contact(string name, string id, string number){ contactname = name; contactid = id; contactnumber = number; } } public class myclassadapter extends arrayadapter<contact> { private class viewholder { private textview name; private textview id; private textview number; } public myclassadapter(context context, int textviewresourceid, arraylist<contact> items) { super(context, textviewresourceid, items); } public view getview(int position, view convertview, viewgroup parent) { viewholder viewholder; if (convertview == null) { convertview = layoutinflater.from(this.getcontext()) .inflate(r.layout.row_line, parent, false); viewholder = new viewholder(); viewholder.name = (textview) convertview.findviewbyid(r.id.contactname); viewholder.id = (textview) convertview.findviewbyid(r.id.contactid); viewholder.number = (textview) convertview.findviewbyid(r.id.contactnumber); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); } contact item = getitem(position); if (item!= null) { viewholder.name.settext(item.contactname); viewholder.id.settext(item.contactid); viewholder.number.settext(item.contactnumber); } return convertview; } } }
row_line.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:id="@+id/contactname" android:textsize="16sp" android:textstyle="bold" android:layout_width="match_parent" android:layout_height="wrap_content"/> <textview android:id="@+id/contactid" android:textsize="16sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <textview android:id="@+id/contactnumber" android:textsize="16sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </linearlayout>
activity_provider2.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" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".provider2activity"> <listview android:id="@android:id/list" android:layout_height="wrap_content" android:layout_width="match_parent"> </listview> </relativelayout>
Comments
Post a Comment