android--Operation ContentProvider of the contact person

Source: Internet
Author: User

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

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.