Android programming-Simple phone Address Book

Source: Internet
Author: User

In, mobile phone QQ you will find the software to read the phone Address Book this feature, this feature makes the software better with the phone contacts binding, so as to achieve sharing, call, read user information and other operations. Below we will implement this function through a demo

First we look at:

-----------------------------------------------------------the "to-do section"---------------------------------------------- -

"Development environment"Android Stdio 1.1.0

Analysis

The software contains a ListView control, which is one of the most practical controls in Android programming, customizing the layout of the item and the data in the control from the contents of your phone's address book. So the adapter chooses:cursoradapter or baseadapter, The small part uses the CursorAdapter, obtains the data and assigns to Item , Another important point in the demo is to get access to the phone's address book

Encoding

(1) layout of the code is not a small part of the solution, specifically see the following demo of the article

(2) Contact information entity class

Package cn.edu.bzu.contacts_listview;/** * Created by monster on 2015/4/26. * Contact Entity class * Function: Implements the people contact entity class */public class MyFile {public    String peoplename;    public int imgid;    Public String Peoplenumber;    Public MyFile (string peoplename, int imgid, string peoplenumber) {        super ();        Peoplename = Peoplename;        Imgid = Imgid;        Peoplenumber = Peoplenumber;    }    Public MyFile () {        super ();    }}

(3) Adapter for contact list

/** * Created by monster on 2015/4/26. */public class Peoplelistadapter extends CursorAdapter {private Layoutinflater minflater;//define Layoutinflater object Publi        C Peoplelistadapter (context context, Cursor c) {Super (context, c);    Minflater=layoutinflater.from (context);        } @Override public view Newview (context context, cursor cursor, viewgroup parent) {//Initialize view of each item    Return Minflater.inflate (R.layout.activity_list_item,parent,false);  } @Override public void BindView (view view, context context, cursor cursor) {//sets the layout effect of item Viewholder        Viewholder=new Viewholder ();        Viewholder.peopleimage= (ImageView) View.findviewbyid (r.id.ivnews);        Viewholder.peoplename= (TextView) View.findviewbyid (r.id.tvnewstitle);        Viewholder.peoplenumber= (TextView) View.findviewbyid (r.id.tvnewsinfo);        Read Data ViewHolder.PeopleImage.setBackgroundResource (r.mipmap.ic_launcher) from phone contacts; ViewHolder.PeopleName.setText (cursor.getsTring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))); ViewHolder.PeopleNumber.setText (Cursor.getstring (Cursor.getcolumnindex (    (ContactsContract.CommonDataKinds.Phone.NUMBER)));        } class viewholder{ImageView peopleimage;        TextView Peoplename;    TextView Peoplenumber; }}

(4) writing Span style= "font-family:comic Sans MS;" >mainactivity.java

Package Cn.edu.bzu.contacts_listview;import Android.app.activity;import Android.database.cursor;import Android.os.bundle;import Android.provider.contactscontract;import Android.widget.listview;public Class    Mainactivity extends Activity {private ListView Lv;    Private Peoplelistadapter adapter; Private cursor cursor;//defines the cursor @Override protected void onCreate (Bundle savedinstancestate) {s) used to store objects that need to be displayed.        Uper.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Findview ();//Control instantiation SetData ();//Set Data SETLV ();//bind adapter} private void Setlv () {adapter=new peop        Lelistadapter (Mainactivity.this,cursor);    Lv.setadapter (adapter); private void SetData () {//Getcontentresolver gets the contact information from the phone cursor=getcontentresolver (). Query (Contactscon Tract.    Commondatakinds.phone.content_uri,null,null,null,null);    } private void Findview () {lv= (ListView) Findviewbyid (r.id.lv); }}

(5) Set permissions in the manifest file

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

----------------------------------------------------"Summary section"----------------------------------------------------- -

Summary

The adapter inherits the CursorAdapter adapter: The three methods that must be implemented are:

Public Peoplelistadapter (context context, Cursor c)

public view Newview (context context, cursor cursor, viewgroup parent)--->> initialize view of each item

public void BindView (view view, context context, cursor cursor)---->> set The layout effect of Item

(Read the data from the phone's contact person)

Cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))--->> Code to read the contact's name

Cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER))--->> read code for contact numbers

Appendix

Source code Address:Https://github.com/monsterLin/contacts_listview

Android programming-Simple phone Address Book

Related Article

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.