android 读取通讯录显示到gridview
...........
<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的更多相关文章
- android读取通讯录和使用系统通讯录
第一步:注册权限 <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <us ...
- Android根据联系人姓名首字符顺序读取通讯录
Android根据联系人姓名首字符顺序读取通讯录 版权声明:本文为Zhang Phil原创文章,欢迎转载!转载请注明出处:http://blog.csdn.net/zhangphil 本文给出了A ...
- 读取Excel数据绑定到Gridview进行显示
读取Excel数据绑定到Gridview进行显示示例代码. 读取excel代码 /// <summary> /// 读取Excel /// authon:codeo.cn /// < ...
- 使用LINQ TO XML 创建xml文档,以及读取xml文档把内容显示到GridView例子
首先,准备了一个Model类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- android中读取通讯录学习笔记
1.读取通讯录时一次读取时,尽量少读取全部属性.特别是列表展示的时候.会让你的列表载入速度变得难以忍受,建议先载入少量属性.然后在详情的时候载入全部属性. 2.在读取一类属性的时候,建议用一个游标,且 ...
- Android开发:ScrollView嵌套GridView的解决办法
Android开发:ScrollView嵌套GridView的解决办法 前些日子在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便 ...
- Android 自学之网格试图(GridView)和图片切换器(ImageSwitcher)功能和用法
网格试图(GridView)用于在界面上按行,列分布的方式来显示多个组件. GridView和ListView有共同的父类:AbsListView,因此GridView和ListView具有一定的相似 ...
- Android - 读取JSON文件数据
Android读取JSON文件数据 JSON - JavaScript Object Notation 是一种存储和交换文本信息的语法. JSON对象在花括号中书写.用逗号来分隔值. JSON数组在方 ...
- c#读取Excel数据到Gridview
#region 读取Excel数据到Gridview public void ReadExcel(string sExcelFile, GridView dgBom) { DataTable E ...
随机推荐
- java.lang下面有一个接口:Comparable(可比较的)
对于自定义对象,Sort不知道规则,所以无法比较.这种情况下一定要定义排序规则.方式有两种: java.lang下面有一个接口:Comparable(可比较的) 可以让自定义对象实现一个接口,这个接口 ...
- 测试RESTful API利器-Postman
对于前端开发者而言,最需要的往往不是技术本身,其实技术都没什么难的,而最缺少的则是各种各样好的兵器,比如调试,开发工具等等. 我们这里就推荐一款前端开发的利器-Postman,它是Google Chr ...
- Spring_day04--SSH框架整合过程
SSH框架整合过程 第一步 导入jar包 第二步 搭建struts2环境 (1)创建action,创建struts.xml配置文件,配置action (2)配置struts2的过滤器 第三步 搭建hi ...
- sqlserver 安全
1.将数据库的用户名和密码加密保存,使用加密传输.2.将数据库里面的用户除了这个用户所有的用户都禁用,把该用户的密码改的很复杂,很难破解那种3.设置数据库的可连接方式(所有的方式的设置).4.删除数据 ...
- JMS和ActiveMQ的关系
JMS和MQ的关系: JMS是一个用于提供消息服务的技术规范,它制定了在整个消息服务提供过程中的所有数据结构和交互流程.而MQ则是消息队列服务,是面向消息中间件(MOM)的最终实现,是真正的服务提供者 ...
- JZOJ.5306【NOIP2017模拟8.18】棋盘游戏
Description 这个游戏上在一个无限大的棋盘上, 棋盘上只有一颗棋子在位置(x,y)(x,y>=0)棋盘的左下角是(0,0)Amphetamine每次都是第一个移动棋子,然后Amphet ...
- 帧动画和骨骼json、极速、二进制对比
对比总结: 1. 帧动画的效率最高,但是图片超过一定帧数,资源图片非常大.比较适合帧数少,大量动画存在,要求效率高的场合. 骨骼json效率较低,已经不推荐使用. 骨骼极速,不支持网格等. 骨骼二进制 ...
- Swift 实现单例模式Singleton pattern的三种方法
转自:点击打开链接 From my short experience with Swift there are three approaches to implement the Singleton ...
- maven web框架搭建
前面我们描述了如何使用maven搭建一个最基本的Java Project 框架.在实际生产应用中,一般不会仅仅建立一个最基本的Java Project项目,目前 Java Web 开发已经成为Java ...
- chrome/FF 解析遇到 { 行为一致,返回不一致
测试的时候,发现一个问题,FF下: chrome 下: 你会发现,FF 在解析一直到返回的时候,都是把 {x:1} 当做一个语句块去解析的,而 chrome 在返回的时候返回了对象,把 {x:1} 当 ...