...........

<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. Android(安卓)Contacts(一)—— 读取联系人
  2. android https之二
  3. android https之二
  4. android 仿微信demo————微信通讯录界面功能实现(移动端,服务端
  5. Android(安卓)数据的读取与写入2
  6. Android字母索引、Android通讯录索引、Android地区索引、Android
  7. logstash用kafka作为输入源
  8. ELK 安装配置
  9. SpringBoot中Bean自动装配原理

随机推荐

  1. android 访问 OData
  2. 解决Fetching android sdk component inf
  3. Android 常用开发术语
  4. Android Studio 单刷《第一行代码》系列
  5. Android系统简介
  6. Android状态栏
  7. Android Studio导入Project的方法
  8. Android 常用技术总览
  9. Android应用开发之android菜单
  10. Flutter插件开发之APK自动安装