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. See 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 will be broken into 3 sections. First covering accessing contacts in Android 2.0. The second page will deal with accessing the contacts in Android 1.6 and before. Third we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records.


Contacts 读取代码:

[java]  view plain copy print ?
  1. package com.homer.phone;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.database.Cursor;  
  8. import android.os.Bundle;  
  9. import android.provider.ContactsContract;  
  10. import android.provider.ContactsContract.CommonDataKinds.Phone;  
  11. import android.widget.ListView;  
  12. import android.widget.SimpleAdapter;  
  13.   
  14. public class phoneRead extends Activity {  
  15.         
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState){  
  18.         super.onCreate(savedInstanceState);  
  19.           
  20.         showListView();  
  21.     }  
  22.       
  23.     private void showListView(){  
  24.         ListView listView = new ListView(this);  
  25.           
  26.         ArrayList> list = getPeopleInPhone2();  
  27.         SimpleAdapter adapter = new SimpleAdapter(  
  28.                                     this,   
  29.                                     list,   
  30.                                     android.R.layout.simple_list_item_2,   
  31.                                     new String[] {"peopleName""phoneNum"},   
  32.                                     new int[]{android.R.id.text1, android.R.id.text2}  
  33.                                 );  
  34.         listView.setAdapter(adapter);  
  35.           
  36.         setContentView(listView);  
  37.     }  
  38.       
  39.     private ArrayList> getPeopleInPhone2(){  
  40.         ArrayList> list = new ArrayList>();  
  41.           
  42.         Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, nullnullnullnull);     // 获取手机联系人  
  43.         while (cursor.moveToNext()) {  
  44.             HashMap map = new HashMap();  
  45.               
  46.             int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);    // people name  
  47.             int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER);            // phone number  
  48.   
  49.             String strPeopleName = cursor.getString(indexPeopleName);  
  50.             String strPhoneNum = cursor.getString(indexPhoneNum);  
  51.   
  52.             map.put("peopleName", strPeopleName);  
  53.             map.put("phoneNum", strPhoneNum);  
  54.             list.add(map);  
  55.         }  
  56.         if(!cursor.isClosed()){  
  57.             cursor.close();  
  58.             cursor = null;  
  59.         }  
  60.           
  61.         return list;  
  62.     }  
  63. }  

AndroidManifest.xml 权限

 记得在AndroidManifest.xml中加入android.permission.READ_CONTACTS这个permission


运行结果:




转自:http://blog.csdn.net/ithomer/article/details/7328590


更多相关文章

  1. android中listview中去除背景色选中色
  2. android http 请求方式
  3. linux ubuntu android SDK安装
  4. Android(安卓)Edittext获取焦点后,弹出的软键盘显示搜索、发送、
  5. Android(安卓)TextView跑马灯效果
  6. android https之三
  7. android https之三
  8. android获取手机信息大全
  9. 11、WebView 使用注意问题

随机推荐

  1. Android(安卓)手机状态
  2. Android(安卓)HFP Profile 连接过程
  3. android滑动删除的一个开源项目SwipeDelM
  4. Android(安卓)P Wifi Enable 之后扫描流
  5. Android(安卓)中的 requestWindowFeature
  6. android:SQLiteOpenHelper
  7. BackHandler是全局的!!!
  8. ScrollView去掉半月阴影
  9. Using Android(安卓)Debug Bridge (ADB)
  10. android 把图变成灰色