Android ListView实现通讯录的实例

来源:互联网 发布:二战苏联单兵装备知乎 编辑:程序博客网 时间:2024/04/28 06:51

该Demo实战项目主要演示了如何从手机中获取联系人手机号及联系人名称。


  • ListView的自定义适配器使用方法
  • 内容解析器ContentResolver的使用方法
  • 数据封装
内容比较简单,但详细地演示了把数据绑定到ListView并显示出来的效果。

项目UI界面实现:ListView控件
项目逻辑实现:先获取手机联系人及手机号码信息,然后封装到实体类中,最后将实体类数据通过ListView显示

效果演示如下:

首先,我们先获取联系人信息,需要获取的有手机号和联系人。
第一步:获取Cursor游标对象

Cursor cursor=context.getContentResolver().query(CoONTENT_URI,null,null,null,null);

第二步:
通过游标遍历所有的数据

cursor.moveToNext()

获取联系人代码如下

GetPhoneContacts.java

package com.mero.ceshi;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.database.Cursor;import android.provider.ContactsContract.CommonDataKinds.Phone;public class GetPhoneContacts {public static List<PhoneEntity> list=new ArrayList<PhoneEntity>();public static String getNumber(Context context){Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);String phoneNumber;String phoneName;//while(cursor.moveToNext()){}这两种遍历方式一样for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));phoneName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));System.out.println("phoneNumber:"+phoneNumber+"phoneName:"+phoneName);PhoneEntity phoneEntity=new PhoneEntity(phoneNumber, phoneName);list.add(phoneEntity);}return null;}

通过以上代码,我们使用getNumber方法把联系人和手机号封装进了类型为PhoneEntity的List中。
然后,看到我们的实体封装类,分别实现其Getter和Setter方法。
PhoneEntity.java
package com.mero.ceshi;public class PhoneEntity {private String phoneNumber;//手机号private String phoneName;//手机联系人PhoneEntity(String phoneNumber,String phoneName){this.phoneName=phoneName;this.phoneNumber = phoneNumber;}/** * @return the phoneNumber */public String getPhoneNumber() {return phoneNumber;}/** * @param phoneNumber the phoneNumber to set */public void setPhoneNumber(String phoneNumber) {this.phoneNumber = phoneNumber;}/** * @return the phoneName */public String getPhoneName() {return phoneName;}/** * @param phoneName the phoneName to set */public void setPhoneName(String phoneName) {this.phoneName = phoneName;}}


接下来就是我们的适配器。我们采用的是继承BaseAdapter。这样就可以自己定义适配器的布局了。创建一个MyAdapter
MyAdapter.java

package com.mero.ceshi;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.TextView;public class MyAdapter extends BaseAdapter{private Context context;private List<PhoneEntity> lists;private LinearLayout layout;public MyAdapter(Context context,List<PhoneEntity> lists){ this.context=context; this.lists=lists;}//返回列表的显示项,跟传进来的list数据数目一致。@Overridepublic int getCount() {return lists.size();}/* (non-Javadoc) * @see android.widget.Adapter#getItem(int) *///返回当前项@Overridepublic Object getItem(int position) {return lists.get(position);}/* (non-Javadoc) * @see android.widget.Adapter#getItemId(int) *///返回当前项的索引@Overridepublic long getItemId(int position) {return position;}/* (non-Javadoc) * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) *///得到布局视图@Overridepublic View getView(int position, View convertView, ViewGroup parent) {//得到布局加载器LayoutInflater inflater = LayoutInflater.from(context);//加载得到item每一项的布局layout = (LinearLayout) inflater.inflate(R.layout.list_item_phone, null);//通过视图得到控件TextView phoneNumTv = (TextView) layout.findViewById(R.id.tx_phoneNumber);TextView phoneNameTv = (TextView) layout.findViewById(R.id.tx_phoneName);//为控件填充数据phoneNumTv.setText(lists.get(position).getPhoneNumber());phoneNameTv.setText(lists.get(position).getPhoneName());return layout;}}
接下来就是我们的item项的布局文件。记得把字符串提取到strings.xml文件中,快捷键提示是ctrl+1。

list_item_phone.xml

<?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="wrap_content"        android:orientation="horizontal">        <ImageView             android:id="@+id/image1"            android:layout_width="45dp"            android:layout_height="45dp"            android:src="@drawable/ic_launcher"/>        <LinearLayout             android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            android:layout_gravity="center_vertical">            <TextView                 android:id="@+id/tx_phoneName"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="联系人"                android:textSize="17sp"></TextView>            <TextView android:id="@+id/tx_phoneNumber"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="手机号码"></TextView>            </LinearLayout>    </LinearLayout></LinearLayout>


最后,就是实例化我们的ListView并将适配器数据绑定到其上面去。代码如下:

package com.mero.ceshi;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;public class MainActivity extends Activity {private ListView ls;private MyAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        GetPhoneContacts.getNumber(this);//调用getNumber方法,将数据封装到List<PhoneEntity>集合中。        ls=(ListView) findViewById(R.id.list_phone);//得到ListView对象        adapter=new MyAdapter(this, GetPhoneContacts.list);//得到适配器对象        ls.setAdapter(adapter);//为listView设置适配器    }}

好了,通过上面的演示,我们成功地实现了通讯录的Demo。谢谢阅读。

1 0
原创粉丝点击