Add several contacts in the emulator and export the database under Data/data/, as follows:
Open it.
The list of concerns is: Raw_contact,data,mimetypes, Count three sheets.
First get content parser, get content parser, can use Query method, query need to pass in URI, view source code see the path of raw_contacts.
Resolver.query (URI, new string[]{"contact_id"}, Selection, Selectionargs, SortOrder);
The second argument to query corresponds to the Raw_contacts table, and we only care about the contact's ID:
All other parameters are passed in null. The results of the query are returned as cursor, and the cursor is MoveToNext loop to get contact_id:
String contact_id = cursor.getstring (0);
According to the obtained contact_id, the query corresponds to the data table.
Cursor datacursor = Resolver.query (uridata, New string[]{"Data1"}, "Contact_id=?", New string[]{contact_id}, NULL);
Save the final data to the map, add the map to the list, and return to the list.
Layout of the main interface:
<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= "${relativepackage}.${activityclass}" > <listview android:id= "@+id/contact" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "@ Android:color/transparent " android:cachecolorhint=" #00000000 " android:descendantfocusability=" Afterdescendants " android:divider=" #cccccc " android:dividerheight=" 1px " android: Fastscrollenabled= "false"/></relativelayout>
Layout of each ListItem:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <imageview Android:id= "@+id/iv_image" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:src= "@drawable/default_avatar"/> <textview android:layout_torightof= "@id/iv_ima GE "android:id=" @+id/name "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:text= "name" android:textsize= "18SP" android:layout_marginleft= "10DP" android:textcolor= "#88000000"/> <textview android:textcolor= "#88000000" android:textsize= "18SP" a Ndroid:id= "@+id/phone" android:layout_torightof= "@id/iv_image" android:layout_below= "@id/name" Android Oid:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "15205553932" android:layout_marginleft= "10DP" /> </RelativeLayout>
Logic code:
Package Com.dy.ustc.readcontact;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.activity;import Android.content.contentresolver;import android.database.Cursor; Import Android.net.uri;import android.os.bundle;import android.widget.imageview;import Android.widget.ListView; Import Android.widget.simpleadapter;import Android.widget.textview;public class Mainactivity extends Activity { Private ListView contact;private ImageView iv_image;private TextView name;private TextView phone; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); contact = (ListView) Findviewbyid (r.id.contact); list<map<string, string>> data = Getcontactinfo (); Contact.setadapter (New Simpleadapter (this, data, R.layout.list_item, new string[]{"name", "Phone"}, new int[]{ R.id.name,r.id.phone}));} /** * * Read Phone contact information * * @return */private list<map<string, string>> Getcontactinfo () {//list<map<string all contacts, string>> list = new arraylist<map<string,string >> ();//Get a content parser contentresolver resolver = Getcontentresolver ();//raw_contacts This table's path uri uri = Uri.parse (" Content://com.android.contacts/raw_contacts ");//The path Uri of the data table Uridata = Uri.parse (" content://com.android.contacts/ Data "); Uri Uri_mimetype = Uri.parse ("Content://com.android.contacts/mimetype"); cursor cursor = resolver.query (URI, new string[] {"contact_id"},null, NULL, NULL), while (Cursor.movetonext ()) {String Co ntact_id = cursor.getstring (0); if (contact_id! = null) {//specific one of the contacts map<string, string> map = new hashmap<string, String> (); Cursor datacursor = Resolver.query (uridata,new string[] {"Data1", "MimeType"}, "Contact_id=?", new string[] {contact_id} , null), while (Datacursor.movetonext ()) {String data1 = datacursor.getstring (0);//system.out.println (data1); String mimetype = datacursor.getstring (1); System.out.println (MimeType + ":" + data1), if ("VND.Android.cursor.item/name ". Equals (MimeType)) {///Contact name Map.put (" name ", data1);} else if ("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) {//contact person's phone number map.put ("Phone", data1);}} List.add (map);d atacursor.close ();}} Cursor.close (); return list;}}
Run to get the following result:
22 Reading Contacts