"Android Demo" simple phone Address Book

Source: Internet
Author: User

The Android system gives us access to the Address Book interface, through the interface to obtain address book information. The connection between Adapter and View relies primarily on the GetView method to return the custom view we need. The ListView is one of the most common controls in the Android App, so it's important to get a good user experience by getting the ListView running smoothly. The optimization of ListView is to optimize the GetView method in Adapter.

Core content:

1. Get Phone Contacts

2. Data encapsulation

3. Create Adapter

4. Optimizing the Adapter

Development environment: Eclipse

Steps:

1. Create a new project, create a new Getnumber.class class to get information from your contacts

 PackageCom.yanis.getmyphonenumber;Importjava.util.ArrayList;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.database.Cursor;ImportAndroid.provider.ContactsContract.CommonDataKinds.Phone; Public classGetNumber { Public Staticlist<phoneinfo> lists =NewArraylist<phoneinfo>();  Public StaticString GetNumber (context context) {cursor cursor=context.getcontentresolver (). Query (Phone.content_uri,NULL,NULL,NULL,NULL);        String PhoneNumber;        String Phonename;  while(Cursor.movetonext ()) {PhoneNumber= Cursor.getstring (Cursor.getcolumnindex (Phone.number));//Phone numberPhonename = cursor.getstring (Cursor.getcolumnindex (phone.display_name));//namePhoneinfo info =NewPhoneinfo (Phonename, PhoneNumber);            Lists.add (info); System.out.println (Phonename+PhoneNumber); }        return NULL; }}

2. Add Permissions

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

3. New Phoneinfo.class class as Address Book information encapsulation class

 PackageCom.yanis.getmyphonenumber;/** *  * @authorYechao * @ Description Address Book information Encapsulation class*/ Public classPhoneinfo {PrivateString Phonename; PrivateString PhoneNumber;  Publicphoneinfo (String phonename, String phonenumber) {setphonename (phonename);    Setphonenumber (PhoneNumber); }     PublicString Getphonename () {returnPhonename; }     Public voidsetphonename (String phonename) { This. Phonename =Phonename; }     PublicString Getphonenumber () {returnPhoneNumber; }     Public voidSetphonenumber (String phonenumber) { This. PhoneNumber =PhoneNumber; }}

4. Customizing the adapter class for the ListView Myadapter.class

 PackageCom.yanis.getmyphonenumber;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView; Public classMyadapterextendsBaseadapter {PrivateList<phoneinfo>lists; PrivateContext context;//undertake context         PublicMyadapter (list<phoneinfo>Lists,context Context) {         This. Lists =lists;  This. Context =context; }        //returns the number of collections@Override Public intGetCount () {returnlists.size (); }    //return Current Data@Override PublicObject GetItem (intposition) {        returnLists.get (position); }    //Get current ID@Override Public LongGetitemid (intposition) {        returnposition; }    //return to Current View@Override PublicView GetView (intposition, View Convertview, ViewGroup parent)        {Viewholder holder; if(convertview==NULL) {Convertview= Layoutinflater.from (context). Inflate (R.layout.call,NULL); Holder=NewViewholder (); Holder.tvname=(TextView) Convertview.findviewbyid (r.id.tv_name); Holder.tvnumber=(TextView) Convertview.findviewbyid (R.id.tv_number);        Convertview.settag (holder); }Else{Holder=(Viewholder) Convertview.gettag ();        } holder.tvName.setText (Lists.get (position). Getphonename ());        Holder.tvNumber.setText (Lists.get (position). Getphonenumber ()); returnConvertview; }        Private Static classviewholder{TextView Tvname;    TextView Tvnumber; }}

5. Finally is the main interface code, as far as the layout is too simple to list, interested children shoes can view the source code

 PackageCom.yanis.getmyphonenumber;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.ListView; Public classMainactivityextendsActivity {PrivateListView ListView; PrivateMyadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Getnumber.getnumber ( This);    Initview (); }    Private voidInitview () {ListView=(ListView) Findviewbyid (R.id.listview); Adapter=NewMyadapter (Getnumber.lists, This);    Listview.setadapter (adapter); }}

Source code Address: Https://github.com/YeXiaoChao/GetMyPhoneNumber

This address: http://www.cnblogs.com/yc-755909659/p/4312221.html

"Android Demo" simple phone Address Book

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.