...........

<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="47dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:numColumns="1" >
</GridView>

.................

contact_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:paddingBottom="4dip"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="TextView" />

<TextView
android:id="@+id/tvPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/tvName"
android:text="TextView" />

<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/tvPhone"
android:text="TextView" />

</RelativeLayout>

读取通讯录信息 返回ArrayList<HashMap<String,Object>>

public ArrayList<HashMap<String,Object>> readContacts() {

ArrayList<HashMap<String,Object>> lstMap = new ArrayList<HashMap<String,Object>>();

Cursor cursor = this.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

int contactIdIndex = 0;

int nameIndex = 0;

if (cursor.getCount() > 0) {

contactIdIndex = cursor
.getColumnIndex(ContactsContract.Contacts._ID);

nameIndex = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

}

while (cursor.moveToNext()) {

HashMap<String,Object> user = new HashMap<String, Object>();

String contactId = cursor.getString(contactIdIndex);

String name = cursor.getString(nameIndex);

user.put("UserName", name);

//Toast.makeText(this, name, 1000).show();

Cursor phones = this.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[] { contactId }, null);

if (phones.moveToNext()) {

int phoneIndex = phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

String phoneNumber = phones.getString(phoneIndex);

user.put("Telephone", phoneNumber);

//Toast.makeText(this, phoneNumber, 1000).show();
}

phones.close();

Cursor email = this.getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
new String[] { contactId }, null);

if (email.moveToNext()) {

int emailIndex = email
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);

String emailAddress = email.getString(emailIndex);

user.put("EMail", emailAddress);

//Toast.makeText(this, emailAddress, 1000).show();
}

email.close();

lstMap.add(user);

//user.clear();

}

return lstMap;

}

//装配到GridView

final GridView gv=(GridView)this.findViewById(R.id.gridView1);

.......

ArrayList<HashMap<String, Object>> userList = readContacts();

SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, userList,
R.layout.contact_item, new String[] { "UserName",
"Telephone", "EMail" }, new int[] {R.id.tvName,R.id.tvPhone,R.id.tvEmail});

gv.setAdapter(simpleAdapter);

//点击显示姓名

gv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {

// TODO Auto-generated method stub
HashMap <String,Object> map =(HashMap <String,Object>)arg0.getItemAtPosition(arg2);

Toast.makeText(MainActivity.this, map.get("UserName").toString(), 1000).show();

}

});

最新文章

  1. python1day
  2. MySQL 利用SQL线程对Binlog操作
  3. Python-S13作业-day1-之登陆程序
  4. Windows Internals学习笔记(一)概念与工具
  5. Windows python 安装 nNumpy、Scipy、matplotlib模块
  6. 读取svg图片为UIBezierPath,开心做动画
  7. 转载:mysql 对于百万 千万级数据的分表实现方法
  8. 前端资源多个产品整站一键打包&amp;包版本管理(一)
  9. Case swapping
  10. MFC对话框程序EDIT类控件的自动换行,垂直滚动条自动下移
  11. 如何在Linux上检查SSH的版本(转)
  12. 标准IO和重定向
  13. Qt中的View Model模型
  14. ACM HDU Bone Collector 01背包
  15. [HNOI 2011]XOR和路径
  16. springMVC中的注解@RequestParam与@PathVariable的区别
  17. DDoS攻击、CC攻击的攻击方式和防御方法
  18. bitbucket迁移
  19. 【亲测】关于HTTP协议~
  20. 在2018年如何优雅的开发一个typescript语言的npm包?

热门文章

  1. pydoc介绍
  2. gcc编译程序的流程
  3. m2014_c-&gt;c语言容器类工具列
  4. axios post传参后台无法接收问题
  5. Java调用http保留访问状态
  6. 170418、vmware 安装 centos 开启网卡自启动
  7. 160229-01、web页面常用功能js实现
  8. Vue.js_础学习之DOM操作
  9. 浏览器加载不上css,样式走丢
  10. Codeforces Round #427 (Div. 2)—A,B,C,D题