The 1.main.xml code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <textview android:id= "@+id/info" android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Phone contact list" android:textsize= "20px"/> < ListView android:id= "@+id/contactlists" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content"/></linearlayout>
The 2.contacts.xml code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><tablelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <TableRow> <textview android:id= "@+id/_id" android:layout_ Width= "60px" android:layout_height= "wrap_content" android:textsize= "15px"/> <textview Android:id= "@+id/name" android:layout_width= "300px" android:layout_height= "Wrap_content " Android:textsize= "15px"/> </TableRow></TableLayout>
The 3..java code is as follows:
Package Org.lxh.demo;import Java.util.arraylist;import Java.util.hashmap;import java.util.list;import java.util.Map ; Import Android.app.activity;import Android.database.cursor;import Android.os.bundle;import Android.provider.contactscontract;import Android.widget.listview;import Android.widget.simpleadapter;public Class Contactsdemo extends Activity {private Cursor result = NULL;//Since you are querying, the query returns the result private ListView contactslist = null;//definition ListView Component Private List<map<string,object>> allcontacts = null;p rivate simpleadapter simple = null; overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Super.setcontentview ( R.layout.main); this.contactslist = (ListView) Super.findviewbyid (r.id.contactslist);//Get Component This.result = Super.getcontentresolver (). query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL, NULL);// Query Super.startmanagingcursor (this.result);//Give the result set to container management this.allcontacts = new arraylist<map<string,object> > ();//Instantiate the list set for (This.result.moveToFirst ();!this.result.isafterlast (); This.result.moveToNext ()) {//Remove each content in the result set map <String,Object> contact = new Hashmap<string,object> (); Contact.put ("_id", This.result.getInt ( This.result.getColumnIndex (contactscontract.contacts._id)); Contact.put ("Name", This.result.getString ( This.result.getColumnIndex (ContactsContract.Contacts.DISPLAY_NAME)); This.allContacts.add (contact);} This.simple = new Simpleadapter (this, this.allcontacts,r.layout.contacts, new string[] {"_id", "Name"}, new int[] {r.id. _id, r.id.name}); This.contactsList.setAdapter (this.simple);}}
Shown below:
android--Operation ContentProvider of the contact person