Android reads phone contacts and displays ListView

Source: Internet
Author: User

there is one in the tenderness register, I have opened the permission:This principle:1, if the address book is empty, the tenderness will not let you go down,2, if not open permission, the tenderness will not let you go down,3, if you open the permission and the Address book is empty, the tenderness will not let you go down,4. If you open the permissions and the address book is not empty, the tenderness can let you go down.
Read Address Book Permissions
<!--read Contact permissions--    <uses-permission android:name= "Android.permission.READ_CONTACTS"/>    <!-- Call right--    <uses-permission android:name= "Android.permission.CALL_PHONE"/>


Key code
/** Contact name **/private arraylist<contactentity> mcontacts = new arraylist<contactentity> ();p rivate static Final string[] phones_projection = new string[] {phone.display_name, phone.number, phone.photo_id, Phone.CONTACT_ID};/* * Contact display name **/private static final int phones_display_name_index = 0;/** phone number **/private static final int phones_number_ind EX = 1;/** Avatar ID **/private static final int phones_photo_id_index = 2;/** Contact's ID **/private static final int Phones_contac T_id_index = 3;private void Getphonecontacts () {Contentresolver resolver = getcontentresolver (); try {//Get phone contact cursor Pho Necursor = Resolver.query (phone.content_uri,phones_projection, NULL, NULL, NULL), if (phonecursor! = null) {while ( Phonecursor.movetonext ()) {//Get mobile phone number PhoneNumber = phonecursor.getstring (Phones_number_index);// When the mobile number is empty or an empty field skips the current loop if (Textutils.isempty (phonenumber)) continue;//Gets the contact name string ContactName = Phonecursor.getstring (Phones_display_name_index);//Get contact Idlong ContactID = PhonecursoR.getlong (Phones_contact_id_index);//Get contact with head like Idlong photoid = Phonecursor.getlong (Phones_photo_id_index);// Get in touch with the head like Bitampbitmap Contactphoto = null;//photoid greater than 0 indicates that the contact has an avatar if you do not set an avatar for this person give him a default if (PhotoID > 0) {uri uri = Contentu Ris.withappendedid (contactscontract.contacts.content_uri,contactid); InputStream input = ContactsContract.Contacts.openContactPhotoInputStream (resolver, uri); Contactphoto = Bitmapfactory.decodestream ( input);} else {Contactphoto = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);} Contactentity mcontact = new Contactentity (Contactname,phonenumber, Contactphoto); Mcontacts.add (mContact);} Phonecursor.close ();}} catch (Exception e) {e.printstacktrace ();}}

Contactentity entity Class Code:
Package Com.yqy.yqy_testtxl;import android.graphics.bitmap;/** * Contact information * @author yqy * */public class Contactentity {/** Name **/private string name;/** Contact number **/private string number;/** contact human head like **/private Bitmap photo; @Overridepublic String to String () {return "contactentity [name=" + name + ", number=" + number+ ", photo=" + Photo + "]";}  Public contactentity (string name, string number, Bitmap photo) {super (); this.name = Name;this.number = Number;this.photo = Photo;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetNumber () {return number;} public void Setnumber (String number) {this.number = number;} Public Bitmap Getphoto () {return photo;} public void Setphoto (Bitmap photo) {This.photo = photo;}}


Show to ListView:
private void Initlist () {ListView LV = (ListView) Findviewbyid (r.id.listview1); Lv.setadapter (new Myadapter ());} Class Myadapter extends Baseadapter {@Overridepublic int getcount () {if (mcontacts! = null && mcontacts.size () ; 0) {return mcontacts.size ();} else {return 0;}} @Overridepublic Object getItem (int position) {if (mcontacts! = null && mcontacts.size () > 0) {return mcontacts. Get (position);} else {return null;}} @Overridepublic long getitemid (int arg0) {//TODO auto-generated method Stubreturn arg0;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder = null;if (Convertview = = null) {holder = new Viewholder (); Convertview = Layoutinflater.from (mainactivity.this). Inflate (r.layout.item_contact , null); holder.name = (TextView) Convertview.findviewbyid (r.id.tv_name); holder.number = (TextView) Convertview.findviewbyid (r.id.tv_number); Holder.photo = (ImageView) Convertview.findviewbyid (R.id.iv_photo); Convertview.settag (Holder);} Else{holder = (Viewholder) Convertview.gettag ();} Contactentity contact = mcontacts.get (position) Holder.name.setText (Contact.getname () + ""); Holder.number.setText ( Contact.getnumber () + ""); Holder.photo.setImageBitmap (Contact.getphoto ()); return Convertview;} Class Viewholder {TextView name; TextView number;imageview photo;}}


Mainactivity All code:
Package Com.yqy.yqy_testtxl;import Java.io.inputstream;import Java.util.arraylist;import android.app.Activity; Import Android.content.contentresolver;import Android.content.contenturis;import Android.database.cursor;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.net.uri;import Android.os.Bundle; Import Android.provider.contactscontract;import Android.provider.contactscontract.commondatakinds.phone;import Android.text.textutils;import Android.util.log;import Android.view.layoutinflater;import Android.view.Menu;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.ImageView Import Android.widget.listview;import Android.widget.textview;public class Mainactivity extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); getphonecontacts (); LOG.E ("YQY", mcontacts.size () + "-------------mcontacts---------"); inItlist ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} /** Contact name **/private arraylist<contactentity> mcontacts = new arraylist<contactentity> ();p rivate static Final string[] phones_projection = new string[] {phone.display_name, phone.number, phone.photo_id, Phone.CONTACT_ID};/* * Contact display name **/private static final int phones_display_name_index = 0;/** phone number **/private static final int phones_number_ind EX = 1;/** Avatar ID **/private static final int phones_photo_id_index = 2;/** Contact's ID **/private static final int Phones_contac T_id_index = 3;private void Getphonecontacts () {Contentresolver resolver = getcontentresolver (); try {//Get phone contact cursor Pho Necursor = Resolver.query (phone.content_uri,phones_projection, NULL, NULL, NULL), if (phonecursor! = null) {while ( Phonecursor.movetonext ()) {//Get mobile number string phonenumber = Phonecursor.getstring (phones_numBER_INDEX);//When the mobile number is empty or an empty field skips the current loop if (Textutils.isempty (phonenumber)) continue;//Get the contact name string ContactName = Phonecursor.getstring (Phones_display_name_index);//Get contact Idlong ContactID = Phonecursor.getlong (phones_contact_id_ INDEX);//Get contact head like Idlong photoid = Phonecursor.getlong (Phones_photo_id_index);//Get contact with head like Bitampbitmap Contactphoto = null;//PhotoID greater than 0 indicates that the contact has an avatar if you do not set an avatar for this person give him a default if (PhotoID > 0) {uri uri = Contenturis.withappendedid (Contactscontrac T.contacts.content_uri,contactid); InputStream input = ContactsContract.Contacts.openContactPhotoInputStream ( resolver, uri); Contactphoto = Bitmapfactory.decodestream (input);} else {Contactphoto = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);} Contactentity mcontact = new Contactentity (Contactname,phonenumber, Contactphoto); Mcontacts.add (mContact);} Phonecursor.close ();}} catch (Exception e) {e.printstacktrace ();}} private void Initlist () {ListView LV = (ListView) Findviewbyid (r.id.listview1); Lv.setadapter (New MyadaPter ());} Class Myadapter extends Baseadapter {@Overridepublic int getcount () {if (mcontacts! = null && mcontacts.size () ; 0) {return mcontacts.size ();} else {return 0;}} @Overridepublic Object getItem (int position) {if (mcontacts! = null && mcontacts.size () > 0) {return mcontacts. Get (position);} else {return null;}} @Overridepublic long getitemid (int arg0) {//TODO auto-generated method Stubreturn arg0;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder = null;if (Convertview = = null) {holder = new Viewholder (); Convertview = Layoutinflater.from (mainactivity.this). Inflate (r.layout.item_contact , null); holder.name = (TextView) Convertview.findviewbyid (r.id.tv_name); holder.number = (TextView) Convertview.findviewbyid (r.id.tv_number); Holder.photo = (ImageView) Convertview.findviewbyid (R.id.iv_photo); Convertview.settag (holder);} Else{holder = (Viewholder) Convertview.gettag ();} Contactentity contact = mcontacts.get (position); HoLder.name.setText (Contact.getname () + ""); Holder.number.setText (Contact.getnumber () + ""); Holder.photo.setImageBitmap (Contact.getphoto ()); return Convertview;} Class Viewholder {TextView name; TextView number;imageview Photo;}}}


Activity_main.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= ". Mainactivity ">    <listview        android:id=" @+id/listview1 "        android:layout_width=" Match_parent "        android:layout_height= "wrap_content"        android:layout_alignparentleft= "true"        android:layout_ Alignparenttop= "true" >    </ListView></RelativeLayout>

The operation is not up ...Copy to Google TranslateTranslation Results

Android reads phone contacts and displays ListView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.