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


    }


    });

  • 相关阅读:
    django模型005
    django模型004
    django视图003
    django视图002
    django视图001
    开始学习Django框架,感谢虫师,这里会记录学习的过程和心得!开始Django吧!
    新手超详细的Github教程,MAC版本非windows版本
    GitHub 新手详细教程(转载)
    解决MAC电脑Sequel Pro encountered an unexpected error 问题(转载)
    python用二进制读取文件
  • 原文地址:https://www.cnblogs.com/honeynm/p/3785354.html
Copyright © 2011-2022 走看看