获取手机通讯录
android基本操作--获取手机通讯录。本文简要讲解怎样获取android手机通讯录。并通过List把通讯录显示出来。
主程序如下:
package com.xiaochun91103; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.database.Cursor; import android.os.Bundle; import android.provider.Contacts.People; import android.provider.Contacts.Phones; import android.provider.ContactsContract.Contacts; import android.widget.SimpleAdapter; public class GetContactTest extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter(GetContactTest.this,getData(),R.layout.item,new String[]{ "name","number" },new int[]{ R.id.name,R.id.number }); setListAdapter(adapter); } private ArrayList<? extends Map<String, String>> getData() { // TODO Auto-generated method stub ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>(); String[] columns = new String[] {Phones.NAME,Phones.NUMBER}; Cursor cursor = GetContactTest.this.getContentResolver().query(Phones.CONTENT_URI, columns, null, null, People.DEFAULT_SORT_ORDER); while(cursor.moveToNext()) { Map<String, String> map = new HashMap<String, String>(); map.put("name", cursor.getString(0)); map.put("number", cursor.getString(1)); list.add(map); } return list; } }
布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textSize="20dip" /> <TextView android:id="@+id/number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/name" /> </RelativeLayout>
程序运行截图如下:
更多相关文章
- Android(安卓)中sp、dp、px转换,以及获取手机屏幕宽高
- Android调用手机拍照以及从相册选择照片
- 解决小米手机不能运行Android(安卓)Studio程序的问题
- js判断用户手机是IOS还是安卓
- js判断浏览器所在的客户端类型
- android如何获取手机联系人的数据库
- Android(安卓)实现微信聊天一样的布局
- 【Android】Android(安卓)代码判断是否获取ROOT权限(二)
- 【Android(安卓)工具类】常用工具类(方法)大全