一、添加权限:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 

二、item.xml(创建ListView):

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="60sp"        android:orientation="horizontal" >        <TextView            android:id="@+id/tv_name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:text="1111"            android:textSize="35sp" />        <TextView            android:id="@+id/tv_number"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="bottom"            android:text="2222"            android:textSize="25sp" />    LinearLayout>LinearLayout>

三、activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="match_parent" >    ListView>RelativeLayout>

四、ListViewAdapter.java(ListView的适配器):

package com.example.listviewshowlianxipeople;import java.util.List;import android.annotation.SuppressLint;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;@SuppressLint("InflateParams")public class ListViewAdapter extends BaseAdapter {    List names, phones;    LayoutInflater inflater;    @SuppressWarnings("static-access")    public ListViewAdapter(Context context, List names,            List phones) {        inflater = inflater.from(context);        this.names = names;        this.phones = phones;    }    @Override    public int getCount() {        return names.size();    }    @Override    public Object getItem(int position) {        return position;    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        View view;        if (convertView == null) {            view = inflater.inflate(R.layout.item, null);            TextView tv_name = (TextView) view.findViewById(R.id.tv_name);            TextView tv_phone = (TextView) view.findViewById(R.id.tv_number);            tv_name.setText(names.get(position));            tv_phone.setText(phones.get(position));        } else {            view = convertView;        }        return view;    }}

五、MainActivity.java:

package com.example.listviewshowlianxipeople;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContract;import android.widget.ListView;public class MainActivity extends Activity {    ListView lv;    List list_phone, list_name;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lv = (ListView) findViewById(R.id.lv);        list_name = new ArrayList();        list_phone = new ArrayList();        Cursor c = getContentResolver().query(                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,                null, null);        //获取通讯录的信息        startManagingCursor(c);        int phoneIndex = 0, nameIndex = 0;        if (c.getCount() > 0) {            phoneIndex = c                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);            // 获取手机号码的列名            nameIndex = c                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);            // 获取用户名的列名        }        while (c.moveToNext()) {            String phone = c.getString(phoneIndex);            // 获取手机号码            list_phone.add(phone);            String name = c.getString(nameIndex);            // 获取用户名            list_name.add(name);        }        ListViewAdapter adapter = new ListViewAdapter(this, list_name,                list_phone);        lv.setAdapter(adapter);    }}

代码下载地址:
http://download.csdn.net/detail/zhengyikuangge/9511249

更多相关文章

  1. Android初级教程人品计算器
  2. Android获取剪贴板内容
  3. Android(安卓)AP模式下获取SSID/PASSWORD
  4. android截图
  5. android view相对于根布局的坐标获取
  6. android典型代码系列(十)------获取一个应用程序的权限信息(反射
  7. 【android】getCacheDir()、getFilesDir()、getExternalFilesDir
  8. Android如何获取asset目录下所有文件的路径
  9. android 获取IP

随机推荐

  1. sqlserver四舍五入使用round函数及cast和
  2. 数据库触发器DB2和SqlServer有哪些区别
  3. SQL获取第一条记录的方法(sqlserver、ora
  4. SqlServer异常处理常用步骤
  5. SQL语句执行顺序详解
  6. SQL SERVER 2014 安装图解教程(含SQL SERV
  7. SQLite之Autoincrement关键字(自动递增)
  8. master数据库损坏的解决办法有哪些
  9. 如何把sqlserver数据迁移到mysql数据库及
  10. 修改SQL-SERVER数据库表结构的SQL命令附s