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; }
}