Android_ (Control) using ListView to display contact information in Android system

Source: Internet
Author: User
Tags sqlite database

Use the ListView to display the name and phone number of a contact in your phone

Parent class layout activity_main.xml, subclass layout Line.xml (separate storage for one file)

Run:

(Avoid leaking information to some places for graffiti O (∩_∩) o!)

Program Structure

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.asus.a7gary03" > <!--read Address Book permissions--<uses-permission android:name= "Android.permission.READ_C Ontacts "/> <Application Android:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:roundicon= "@mipmap/ic_launcher_round"Android:supportsrtl= "true"Android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&G                T <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity&    Gt </application></manifest>
Androidmanifest.xml

 Packagecom.example.asus.a7gary03;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.database.Cursor;ImportAndroid.os.Bundle;Importandroid.provider.ContactsContract;ImportAndroid.widget.ListView;Importjava.util.ArrayList;Importjava.util.List; Public classMainactivityextendsappcompatactivity {ListView lv; List<String>List_phone, List_name; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); LV=(ListView) Findviewbyid (r.id.lv); List_name=NewArraylist<string>(); List_phone=NewArraylist<string>(); Cursor C=getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,                NULL,NULL); //get information about your contactsStartmanagingcursor (c); intPhoneindex = 0, Nameindex = 0; if(C.getcount () > 0) {Phoneindex=c. Getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER); //get the column name of the mobile phone numberNameindex =c. Getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); //gets the column name of the user name        }         while(C.movetonext ()) {String phone=c.getstring (Phoneindex); //Get mobile phone numberList_phone.add (phone); String name=c.getstring (Nameindex); //Get user nameList_name.add (name); } Listviewadapter Adapter=NewListviewadapter ( This, List_name, List_phone);    Lv.setadapter (adapter); }}
mainactivity

 Packagecom.example.asus.a7gary03;/*** Created by ASUS on 2018/5/24.*/Importjava.util.List;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.TextView; @SuppressLint ("Inflateparams") Public classListviewadapterextendsBaseadapter {List<String>names, phones;    Layoutinflater Inflater; @SuppressWarnings ("Static-access")     PublicListviewadapter (context context, list<string>names, List<String>phones) {Inflater=Inflater.from (context);  This. Names =names;  This. Phones =phones; } @Override Public intGetCount () {returnnames.size (); } @Override PublicObject GetItem (intposition) {        returnposition; } @Override Public LongGetitemid (intposition) {        returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent)        {View view; if(Convertview = =NULL) {View= Inflater.inflate (R.layout.item,NULL); TextView Tv_name=(TextView) View.findviewbyid (r.id.tv_name); TextView Tv_phone=(TextView) View.findviewbyid (R.id.tv_number);            Tv_name.settext (Names.get (position));        Tv_phone.settext (Phones.get (position)); } Else{View=Convertview; }        returnview; }}
Listviewadapter

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    xmlns:app=" Http://schemas.android.com/apk/res-auto "    xmlns:tools=" http/ Schemas.android.com/tools "    android:layout_width=" Match_parent "    android:layout_height = "Match_parent"    tools:context= "Com.example.asus.a7gary03.MainActivity" >    <ListView        android:id= "@+id/lv"        android:layout_width= "Match_parent"        android:layout_ Height= "match_parent" >    </ListView></RelativeLayout>
Activity_main.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= "Match_parent"android:orientation= "Vertical" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "60SP"android:orientation= "Horizontal" > <TextView Android:id= "@+id/tv_name"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:text= "1111"android:textsize= "20sp"/> <TextView Android:id= "@+id/tv_number"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Bottom"Android:text= "2222"android:textsize= "15sp"/> </LinearLayout></LinearLayout>
Item.xml

First, get the phone memory card permissions

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

Second, the interface layout

Activity_main.xml in Layout

A text box in TextView that displays the phone's contacts, and the ListView lists the contacts in the phone

Item.xml in Layout

Two TextView, one showing the name of the contact, and a phone number showing the contact person

Third, realize the program function

1. Get mobile phone number and contact information.

Cursor C =getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,                NULL,NULL); //get information about your contactsStartmanagingcursor (c); intPhoneindex = 0, Nameindex = 0; if(C.getcount () > 0) {Phoneindex=c. Getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER); //get the column name of the mobile phone numberNameindex =c. Getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); //gets the column name of the user name        }         while(C.movetonext ()) {String phone=c.getstring (Phoneindex); //Get mobile phone numberList_phone.add (phone); String name=c.getstring (Nameindex); //Get user nameList_name.add (name); }

The database used by Cursor:android is a SQLite database, and the cursor can be used for the operation of database records (portal)

Getcolumnindex (String columnName)--------> returns the name of the specified column if there is no return-1

Cursor C = getcontentresolver.query (URI, string[], where, string[], sort);

This statement is believed to be common, and it is easy to find the meaning of five of these parameters by viewing the SDK Help documentation.

The first parameter: is a URI that points to the table that needs to be queried;

The second parameter: the column name that needs to be queried, is an array and can return multiple columns;

The third parameter: the row that needs to be queried, where is the query condition that needs to be met, where can there be? No.

The fourth parameter: is an array that replaces the question mark in the Where statement above;

The fifth parameter: means of sorting;



2. Add Baseadapter Adapter

Simpleadapter Extensibility is best, you can customize a variety of layouts, you can put on imageview (picture), you can also put a button (button), CheckBox (check box) and so on A brief introduction to the ListView and adapter data adapters: Portal
 PublicView GetView (intposition, View Convertview, ViewGroup parent)        {View view; if(Convertview = =NULL) {View= Inflater.inflate (R.layout.item,NULL); TextView Tv_name=(TextView) View.findviewbyid (r.id.tv_name); TextView Tv_phone=(TextView) View.findviewbyid (R.id.tv_number);            Tv_name.settext (Names.get (position));        Tv_phone.settext (Phones.get (position)); } Else{View=Convertview; }        returnview; }

The Layoutinflater class acts like a Findviewbyid ()

The difference is that Layoutinflater is used to find the XML layout file under res/layout/and instantiate

Findviewbyid () is a specific widget control (such as button, TextView, and so on) that is found under the XML layout file.

Android_ (Control) using ListView to display contact information in Android system

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.