zoukankan      html  css  js  c++  java
  • 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();


    }


    });

  • 相关阅读:
    [NOIP2010]关押罪犯
    图的联通入门题
    【luogu4777】扩展中国剩余定理(EXCRT)[数论 扩展中国剩余定理]
    【luogu3868】【TJOI2009】猜数字[模板] [数论 中国剩余定理]
    【luogu1082】【noip2012】同余方程 [数论 扩展欧几里德]
    【luogu1962】斐波那契数列 [矩阵乘法]
    【uva1644】 素数间隔 Prime Gap [数学 质数筛]
    【uva307】小木棍 Sticks [dfs搜索]
    【luogu4011】孤岛营救问题(拯救大兵瑞恩) [最短路][分层思想]
    【noip2015】
  • 原文地址:https://www.cnblogs.com/honeynm/p/3785354.html
Copyright © 2011-2022 走看看