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>

程序运行截图如下:

更多相关文章

  1. Android(安卓)中sp、dp、px转换,以及获取手机屏幕宽高
  2. Android调用手机拍照以及从相册选择照片
  3. 解决小米手机不能运行Android(安卓)Studio程序的问题
  4. js判断用户手机是IOS还是安卓
  5. js判断浏览器所在的客户端类型
  6. android如何获取手机联系人的数据库
  7. Android(安卓)实现微信聊天一样的布局
  8. 【Android】Android(安卓)代码判断是否获取ROOT权限(二)
  9. 【Android(安卓)工具类】常用工具类(方法)大全

随机推荐

  1. php输出xml格式字符串
  2. go语言是脚本语言吗
  3. go是动态语言还是静态语言
  4. godoc命令不存在的解决方法
  5. go语言删除文件的方法
  6. go语言开源么
  7. mac下怎么安装go语言
  8. go语言 类型转换表示什么
  9. yum怎么安装go语言
  10. go语言是动态语言么