...........

<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();

}

});

android 读取通讯录显示到gridview的更多相关文章

  1. android读取通讯录和使用系统通讯录

    第一步:注册权限 <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <us ...

  2. Android根据联系人姓名首字符顺序读取通讯录

      Android根据联系人姓名首字符顺序读取通讯录 版权声明:本文为Zhang Phil原创文章,欢迎转载!转载请注明出处:http://blog.csdn.net/zhangphil 本文给出了A ...

  3. 读取Excel数据绑定到Gridview进行显示

    读取Excel数据绑定到Gridview进行显示示例代码. 读取excel代码 /// <summary> /// 读取Excel /// authon:codeo.cn /// < ...

  4. 使用LINQ TO XML 创建xml文档,以及读取xml文档把内容显示到GridView例子

    首先,准备了一个Model类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  5. android中读取通讯录学习笔记

    1.读取通讯录时一次读取时,尽量少读取全部属性.特别是列表展示的时候.会让你的列表载入速度变得难以忍受,建议先载入少量属性.然后在详情的时候载入全部属性. 2.在读取一类属性的时候,建议用一个游标,且 ...

  6. Android开发:ScrollView嵌套GridView的解决办法

    Android开发:ScrollView嵌套GridView的解决办法   前些日子在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便 ...

  7. Android 自学之网格试图(GridView)和图片切换器(ImageSwitcher)功能和用法

    网格试图(GridView)用于在界面上按行,列分布的方式来显示多个组件. GridView和ListView有共同的父类:AbsListView,因此GridView和ListView具有一定的相似 ...

  8. Android - 读取JSON文件数据

    Android读取JSON文件数据 JSON - JavaScript Object Notation 是一种存储和交换文本信息的语法. JSON对象在花括号中书写.用逗号来分隔值. JSON数组在方 ...

  9. c#读取Excel数据到Gridview

    #region 读取Excel数据到Gridview public void ReadExcel(string sExcelFile, GridView dgBom)    { DataTable E ...

随机推荐

  1. linux_shell_find命令

    使用find查找文件 基本格式:find path expression 1.按照文件名查找 (1)find / -name httpd.conf #在根目录下查找文件httpd.conf,表示在整个 ...

  2. VS2008设置快捷键Ctrl+W关闭当前打开的文本编辑器窗口

    好多友好的软件关闭多标签页的当前页时都有Ctrl+W的快捷键,如Chrome浏览器,使用起来还是很方便的. 但是作为程序员,使用VS2008时有时会打开好多C++或C#源文件,需要关闭某个源文件时你需 ...

  3. java对象和json数据转换实现方式3-使用jackson实现

    測试代码: package com.yanek.util.json; import java.io.IOException; import java.io.StringWriter; import j ...

  4. OSG简单测试框架

    #include <osgDB/ReadFile> #include <osgDB/FileUtils> #include <osg/ArgumentParser> ...

  5. oracle11g安装完成后修改字符集

    author : headsen chen date:2018-05-10  10:27:16 oracle11g完成安装后,由于默认安装的时候无法指定字符集,所以手动修改字符集和10g版本一样的字符 ...

  6. LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  7. dbForge mysql数据库比对

    Comparison选项卡,新建一个表结构比较, (将source库的表结构变化应用到target库) 下面示例中,source用positec_uat,     target用positec_pro ...

  8. 遇到的问题mongodb

    1.MongoNetworkError:failed to connect to server? 数据库没有启动,启动mongo数据库就好 2.有些东西真的是要做好记录的,单纯为了自己日后可以查阅比较 ...

  9. FormsAuthentication 使用指南

    配置安全鉴别 鉴别是指鉴定来访用户是否合法的过程.ASP.NET Framework支持三种鉴别类型: Windows鉴别: NET Passport鉴别: Forms鉴别. 对于某一特定的应用程序, ...

  10. UEFI,BIOS,MBR,

    UEFI启动是一种新的主板引导项,正被看做是有近20多年历史的BIOS 的继任者.顾名思义,快速启动是可以提高开机后操作系统的启动速度.由于开机过程中UEFI的介入 第一:安全性更强 UEFI启动需要 ...