自定义listview读取手机联系人信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aiyuan1996/article/details/52163327

我以手机联系人为例来说明该自定义listview,图片和文字共存。
实现效果如图:
这里写图片描述
下面是我的思路:

main.xml

这个xml很简单,只有一个listview控件。

<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test03_36.MainActivity" >

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv" />

</LinearLayout>

list_item.xml

下面这个list_item.xml是用来定义listview的每个item样式的,很容易看出来,一个image控件和两个Textview控件

<?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="horizontal" >

    <ImageView 
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:gravity="left"
            android:textSize="20sp"/>

        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/phone"
            android:gravity="left"
            android:textSize="10sp"/>
    </LinearLayout>
</LinearLayout>

people.java

此文件定义了一个people实体类,为了方便我们给listview设置数据使用的,listview中的每个item对应了一个people对象。
package com.example.test03_36;

public class People {
    public String peopleName;
    public int ImgId;
    public String peopleNumber;

    public People() {
        super();
    }

    public People(String peopleName, int imgId, String peopleNumber) {
        super();
        this.peopleName = peopleName;
        ImgId = imgId;
        this.peopleNumber = peopleNumber;
    }

}

PeopleListAdapter.java

自定义adapter,继承自CursorAdapter,因为我们adapter的数据源是一个cursor。

CursorAdapter是一个抽象类,需要实现bindView和newView两个抽象方法

package com.example.test03_36;

import android.content.Context;
import android.database.Cursor;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class PeopleListAdapter extends CursorAdapter{
    private LayoutInflater mInflater;

    public String number;

    int i  = 0;
    public PeopleListAdapter(Context context, Cursor c) {
        super(context, c);
        mInflater = LayoutInflater.from(context);
    }

    //设置item页面的布局效果
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = new ViewHolder();
        //获取到对应的控件对象
        viewHolder.peopleImage = (ImageView) view.findViewById(R.id.image);
        viewHolder.PeopleName = (TextView) view.findViewById(R.id.name);
        viewHolder.peopleNumber = (TextView) view.findViewById(R.id.phone);
        //给控件对象设置相应的内容
        viewHolder.peopleImage.setBackgroundResource(R.drawable.ic_launcher);
        viewHolder.PeopleName.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
        viewHolder.peopleNumber.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return mInflater.inflate(R.layout.list_item, parent, false);
    }

    //定义内部类作为占位符组合
    class ViewHolder{
        ImageView peopleImage;
        TextView PeopleName;
        TextView peopleNumber;

    }


}

Mainactivity.java

这里主要是通过getContentResolver方法得到联系人信息的Cursor,然后通过cursor得到PeopleListAdapte的实例adapter,然后listview.setAdapter()就得到了一个自定义联系人listview了。
package com.example.test03_36;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemClickListener {
    private ListView lv;
    private Cursor cursor;
    private PeopleListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        //根据getcontentresolver获取手机联系人的信息
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        //将可选内容与arrayadapter联系起来
        adapter = new PeopleListAdapter(this, cursor);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Toast.makeText(MainActivity.this, view+"", 1).show();
        TextView textview =(TextView) view.findViewById(R.id.phone);
        String number = textview.getText().toString();
        Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));  
        startActivity(intent);  
    }

}

添加权限

当然,我们这读取了联系人信息和打电话,需要添加两个权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>

大功告成
点击下载demo

展开阅读全文

没有更多推荐了,返回首页