android 遍历本地联系人

android2.0以后取得通讯录联系人使用的URI有所变化,改为ContactsContract类,以前的Contacts类已经不推荐使用了。

取得联系人的电话号码是需要先取得_ID, 根据id取得电话号码,并且电话号码存在多个的情况,需要考虑。

具体代码如下:

1. Activity类

[java]  view plain copy
  1. public class AndroidTest extends Activity {  
  2.     private static final String TAG = "AndroidTest";  
  3.     private ListView lv = null;   
  4.     private static final String COLUMN_ID = "No.";// INTEGER PRIMARY KEY     
  5.     private static final String COLUMN_NAME = "name";// TEXT     
  6.     private static final String COLUMN_NUMBER = "number";// INTEGER     
  7.   
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState){  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.         lv = (ListView) this.findViewById(R.id.ListView01);     
  13.           
  14.         // 取得ContentResolver对象     
  15.         ContentResolver cr = getContentResolver();       
  16.         // 取得通讯录的光标     
  17.         String orderBy = PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC";  
  18.         Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, nullnullnull, orderBy);     
  19.   
  20.         // 遍历通讯录  
  21.         ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();  
  22.         for(int i=0; i<cursor.getCount() ;i++)  
  23.         {  
  24.             HashMap<String, Object> map = new HashMap<String, Object>();  
  25.               
  26.             cursor.moveToPosition(i);  
  27.               
  28.             // No.  
  29.             map.put(COLUMN_ID, i + 1);  
  30.               
  31.             // 取得联系人名字     
  32.             int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);     
  33.             String name = cursor.getString(nameFieldColumnIndex);   
  34.             map.put(COLUMN_NAME, name);  
  35.               
  36.             // 取得联系人ID     
  37.             String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));     
  38.             Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  
  39.                     null,  
  40.                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,  
  41.                     nullnull);     
  42.             
  43.             String number = "";  
  44.             // 取得电话号码(可能存在多个号码)     
  45.             for(int j = 0; j < phone.getCount(); j++)    
  46.             {     
  47.                 phone.moveToPosition(j);  
  48.                 String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));     
  49.                 if (j > 0) {  
  50.                     number += " , ";    
  51.                 }  
  52.                 number += strPhoneNumber;   
  53.             }     
  54.             map.put(COLUMN_NUMBER, number);  
  55.             Log.d(TAG, "number = " + number);  
  56.             phone.close();   
  57.               
  58.             listItem.add(map);  
  59.         }  
  60.         cursor.close();  
  61.           
  62.         // 生成适配器的Item和动态数组对应的元素  
  63.         SimpleAdapter listItemAdapter = new SimpleAdapter(this,  
  64.                 listItem,// 数据源  
  65.                 R.layout.list_item,// ListItem的XML实现  
  66.                 // 动态数组与ListItem对应的子项  
  67.                 new String[] { COLUMN_ID, COLUMN_NAME, COLUMN_NUMBER },     
  68.                 new int[] { R.id.TextView1, R.id.TextView2, R.id.TextView3 });  
  69.         lv.setAdapter(listItemAdapter);    
  70.     }  
  71.       
  72. }  

2. main.xml

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/background_color"  
  7.     >     
  8.     <TextView  
  9.         android:layout_width="fill_parent"    
  10.         android:layout_height="wrap_content"  
  11.         android:textColor="#FF4169E1"  
  12.         android:text="通讯录:" />     
  13.     <ListView  
  14.         android:id="@+id/ListView01"  
  15.         android:layout_width="fill_parent"    
  16.         android:layout_height="wrap_content"  
  17.         android:listSelector="@drawable/list_selector_color"  
  18.         android:divider="#808080"  
  19.         android:dividerHeight="1px"  
  20.         android:headerDividersEnabled="true"  
  21.         >  
  22.     </ListView>   
  23. </LinearLayout>  

3. list_item.xml

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"   
  5.     android:layout_height="wrap_content"  
  6.     android:paddingTop="5dip"  
  7.     android:paddingBottom="5dip"  
  8.     android:paddingLeft="5dip"    
  9.     android:gravity="center_vertical"   
  10.     >     
  11.     <TextView  
  12.         android:id="@+id/TextView1"  
  13.         android:layout_width="wrap_content"    
  14.         android:layout_height="wrap_content"   
  15.         android:gravity="center"   
  16.         android:textColor="#FF808080"          
  17.         android:text="" />  
  18.     <LinearLayout  
  19.         android:orientation="vertical"   
  20.         android:layout_width="wrap_content"    
  21.         android:layout_height="wrap_content"  
  22.         android:paddingTop="2dip"  
  23.         android:paddingBottom="2dip"  
  24.         android:paddingLeft="5dip"  
  25.         >  
  26.         <TextView  
  27.             android:id="@+id/TextView2"  
  28.             android:layout_width="wrap_content"    
  29.             android:layout_height="wrap_content"    
  30.             android:textColor="#FF9400D3"  
  31.             android:textSize="20px"  
  32.             android:text="" />  
  33.         <TextView  
  34.             android:id="@+id/TextView3"  
  35.             android:layout_width="wrap_content"    
  36.             android:layout_height="wrap_content"   
  37.             android:textColor="#FF8A2BE2"  
  38.             android:text="" />  
  39.     </LinearLayout>  
  40. </LinearLayout>   

4. background_color.xml用于设置layout背景

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <gradient android:startColor="#FF87CEFA"  
  4.         android:endColor="#FF474747"  
  5.         android:angle="270.0"  
  6.         android:centerY="0.3"  
  7.         android:centerColor="#FFB0E0E6" />   
  8. </shape>  

5. list_selector_color.xml用于设置list item被选中时的颜色

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <gradient android:startColor="#FF90EE90"  
  4.         android:endColor="#FF98FB98"  
  5.         android:angle="270.0"  
  6.         android:centerY="0.3"  
  7.         android:centerColor="#FF00FF7F" />   
  8. </shape>  

 

 画面效果:

预览图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值