Android Contacts (i) Read contacts

Source: Internet
Author: User
Tags sqlite

Introduction to Android Contacts

Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. The Android SQLite and Cursor Article for more information. Google changed the contacts database moving from 1.x to 2.0 versions of Android. This tutorial is broken into 3 sections. The covering accessing contacts in Android 2.0. The second page would deal with accessing the contacts in Android 1.6 and before. Third we'll glue it all together with a class this abstracts specific classes for each version and a set of classes to mans The age of the data from the contact records.

Contacts Read code:

Package com.homer.phone;     
Import java.util.ArrayList;     

Import Java.util.HashMap;     
Import android.app.Activity;     
Import Android.database.Cursor;     
Import Android.os.Bundle;     
Import Android.provider.ContactsContract;     
Import Android.provider.ContactsContract.CommonDataKinds.Phone;     
Import Android.widget.ListView;     

Import Android.widget.SimpleAdapter;     
        public class Phoneread extends activity {@Override public void onCreate (Bundle savedinstancestate) {     

        Super.oncreate (savedinstancestate);     
    Showlistview ();     

        private void Showlistview () {ListView ListView = new ListView (this);     
        arraylist<hashmap<string, string>> list = GetPeopleInPhone2 ();      
                                    Simpleadapter adapter = new Simpleadapter (This, List, Android. R.layout.simple_lIst_item_2, new string[] {"Peoplename", "Phonenum"}, New Int[]{android. R.id.text1, Android.     
        R.ID.TEXT2});     

        Listview.setadapter (adapter);     
    Setcontentview (ListView); Private arraylist<hashmap<string, string>> GetPeopleInPhone2 () {arraylist<hashmap& Lt     

        String, string>> list = new arraylist<hashmap<string, string>> (); Cursor Cursor = Getcontentresolver (). query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL, NULL, NULL, NULL     ); Get mobile phone contact while (Cursor.movetonext ()) {hashmap<string, string> map = new hashmap<     

            String, string> ();    int indexpeoplename = Cursor.getcolumnindex (phone.display_name);            People name int indexphonenum = Cursor.getcolumnindex (Phone.number);Phone number String strpeoplename = cursor.getstring (indexpeoplename);     

            String strphonenum = cursor.getstring (indexphonenum);     
            Map.put ("Peoplename", strpeoplename);     
            Map.put ("Phonenum", strphonenum);     
        List.add (map);     
            } if (!cursor.isclosed ()) {cursor.close ();     
        cursor = NULL;     
    } return list; }
}

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.