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

时间: 2024-08-04 18:25:06

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

android读取通讯录

第一步:注册权限 <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> 第二步:接收的model类 public class ContactModel { private String name; private ArrayL

android 读取手机通讯录并显示listview

脉脉中注册时有一个,我已开启权限: 这个原理: 1.如果通讯录为空,脉脉就不让你往下进行, 2.如果没有开启权限,脉脉就不让你往下进行, 3.如果开启权限且通讯录为空,脉脉就不让你往下进行, 4.如果开启权限且通讯录不为空,脉脉才能让你往下进行. 读取通讯录权限 <!-- 读取联系人权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <!-- 拨打电话权限 -

android中读取通讯录学习笔记

1.读取通讯录时一次读取时,尽量少读取所有属性,特别是列表展示的时候,会让你的列表加载速度变得难以忍受,建议先加载少量属性,然后在详情的时候加载所有属性. 2.在读取一类属性的时候,建议用一个游标,且放在循环外面,能明显加快速度,用projection(表示需要查询的列,在下面代码中是CONTACTOR_ION). 示例代码如下: private static final String[] CONTACTOR_ION = new String[]{ ContactsContract.Common

Android增高第九篇之GridView和SQLite实现分页表格

Android提高第九篇之GridView和SQLite实现分页表格http://blog.csdn.net/hellogv/archive/2010/11/18/6019301.aspx上次讲的Android上的SQLite分页读取,只用文本框显示数据而已,这次就讲得更加深入些,实现并封装一个SQL分页表格控件,不仅支持分页还是以表格的形式展示数据.先来看看本文程序运行的动画: 这个SQL分页表格控件主要分为“表格区”和“分页栏”这两部分,这两部分都是基于GridView实现的.网上介绍And

Android学习之——如何将GridView内嵌在ScrollView中

最近在做一个项目,有一个需求是在ScrollView中内嵌一个GridView. 刚开始,我是以为能直接内嵌在里面: 1 <ScrollView 2 android:layout_width="match_parent" 3 android:layout_height="0dp" 4 android:layout_weight="5.5"> 5 <GridView 6 android:id="@+id/gridView

Android 获取并显示远程图片 Picasso框架的使用(二)

转载请注明出处:明桑Android 在上篇文章中介绍了Picasso的基本用法,这篇文章作为以上的练习: 本文代码github地址:UsePicasso 实现效果:(动图请耐心加载) 需要的知识点 了解Picasso框架的使用:Android 获取并显示远程图片 Picasso框架的使用(一) 知道如何自定义GridView 我们需要编写两个布局文件:activity_gridview.xml activity_main.xml 以及ImageAdapter类.GridImageActivity

Android手机通讯录备份还原代码

实现方法很简单1.把通讯录中的联系人,电话号码保存到txt文件中完成备份.2.读取txt文件,导入到通讯录完成还原. 具体代码1.添加 通讯录读写权限,存储卡写权限 <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission><uses-permission android:name="android.permission.WRITE_EXT

android学习之--网格视图(GridView)和图像切换器(ImageSwitcher)

         GridView用于在界面上按行.列分布显示多个组件.GridView和ListView有共同父类:AbsListView.GridView与ListView的区别在于:ListView只在一个方向上分布,GridView在两个方向上分布.所以使用GridView时一般都指定numColumns大于1,否则该属性默认值为1,就意味着改GridView只有一列,那就变成了ListView GridView的xml属性 android:strtchMode 的值为:      Im

android 读取,写入图片到sd卡源码

<pre name="code" class="html"><!--<span style="font-family: Arial, Helvetica, sans-serif;">在AndroidMainfest.xml文件中添加</span><span style="font-family: Arial, Helvetica, sans-serif;">-->&l