首页 > 代码库 > android 读取通讯录显示到gridview

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


}


});