Android Read contacts

Source: Internet
Author: User

First Step: Register permissions


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

Step two: Receive the Model class

public class Contactmodel {    private String name;    Private arraylist<string > list;    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    Public arraylist<string> getList () {        return list;    }    public void setlist (arraylist<string> list) {        this.list = list;    }}

Step Three: Mian--activity

public class Mainactivity extends appcompatactivity {private button button;    Private ListView ListView;    Static final String TAG = "mainactivity";    Arraylist<contactmodel>datalist = new arraylist<contactmodel> ();        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        ListView = (ListView) Findviewbyid (R.id.contrcts_view);        Readcontacts ();        Mainadapter adapter = new Mainadapter (this,datalist);    Listview.setadapter (adapter);           } private void Readcontacts () {cursor cursor = NULL;                       try {cursor = This.getcontentresolver (). Query (ContactsContract.Contacts.CONTENT_URI,               NULL, NULL, NULL, or NULL);               int contactidindex = 0;               int nameindex = 0; if (Cursor.getcount () > 0) {contactidindex = Cursor.getcolumnindex (Contactscontract.contacTS._ID);               Nameindex = Cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME);               } Contactmodel model = NULL;                   while (Cursor.movetonext ()) {model = new Contactmodel ();                   String contactId = cursor.getstring (Contactidindex);                   String name = cursor.getstring (nameindex);//LOG.I (TAG, contactId);                   LOG.I (TAG, "==============:" + name);            Model.setname (name); /* * Find the contact's phone information */Cursor phones = this.getcontentresolver (). Query (CONTACTSC Ontract. CommonDataKinds.Phone.CONTENT_URI, NULL, Contactscontract.commondataki Nds.                   phone.contact_id + "=" + contactId, NULL, NULL);                   int phoneindex = 0; if (Phones.getcount () > 0) {phoneindex = Phones.getcolumnindeX (ContactsContract.CommonDataKinds.Phone.NUMBER);                   } ArrayList <string>list = new arraylist<string> ();                       while (Phones.movetonext ()) {String PhoneNumber = phones.getstring (Phoneindex);                       LOG.I (TAG, "==============:" + phonenumber);                   List.add (PhoneNumber);                   } model.setlist (list);               Datalist.add (model);           }}catch (Exception e) {log.e (tag,e.tostring ());               }finally {if (cursor! = NULL) {cursor.close (); }           }    }}

  Fourth Step: Adapter

public class Mainadapter extends Baseadapter {private context context;    Private list<contactmodel> List;        Public Mainadapter (context context, list<contactmodel>list) {this.context = context;    This.list = list;    } @Override public int getcount () {return this.list.size ();    } @Override public Object getItem (int i) {return this.list.get (i);    } @Override public long getitemid (int i) {return i;        } @Override public View getView (int i, view view, ViewGroup viewgroup) {Viewholder holder;            if (view = = null) {view = Layoutinflater.from (This.context). Inflate (R.layout.item_main,null);            Holder = new Viewholder ();            Holder.nametv = (TextView) View.findviewbyid (r.id.name);            Holder.phonetv = (TextView) View.findviewbyid (R.id.phone);        View.settag (holder);        }else{holder = (viewholder) view.gettag (); } Contactmodel model = this. List.get (i);        Holder.nameTV.setText (Model.getname ());       StringBuffer buffer = new StringBuffer ();           for (int j=0;j<model.getlist (). Size (); j + +) {if (j==0) {buffer.append (Model.getlist (). Get (j));           }else {buffer.append ("\ n" +model.getlist (). Get (j));        }} holder.phoneTV.setText (Buffer.tostring ());    return view;        } class viewholder{TextView Nametv;    TextView Phonetv; }}

Fifth Step: Item layout

<?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:o rientation= "vertical" >    <textview        android:layout_width= "match_parent"        android:layout_height= " Wrap_content "        android:text=" Tom "        android:layout_margin=" 5DP "        android:id=" @+id/name "/>    < TextView        android:layout_width= "match_parent"        android:layout_height= "wrap_content"        android:text= "18580633453"        android:layout_margin= "5DP"        android:id= "@+id/phone"/>    <view        android: Layout_width= "Match_parent"        android:layout_height= "1DP"        android:background= "#d6d6d6"        /></ Linearlayout>

The layout of main is a ListView, and if not, I need to be quiet.

Android Read contacts

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.