标签:
在微信中,手机QQ中你会发现软件读取手机通讯录这个功能,这个功能使得软件更好的与手机联系人绑定,从而达到分享,拨打电话,读取用户信息等操作。下面我们将通过一个demo实现这个功能
首先我们看一下效果图:
-----------------------------------------------------------【正题部分】-----------------------------------------------
【开发环境】Android Stdio 1.1.0
【分析】
软件中含有一个控件ListView,这个控件可谓是Android程序设计中最最最最实用的一个控件了,自定义item的布局,控件中的数据来源于你的手机通讯录的内容,所以适配器选择:CursorAdapter或者BaseAdapter,小编使用的是CursorAdapter,取得数据并且赋予item中,demo中还需要重要的一点就是获取读取手机通讯录的权限
【编码】
(1)布局方面的代码小编不予细解,具体看文章下面的demo
(2)联系人信息实体类
package cn.edu.bzu.contacts_listview; /** * Created by monster on 2015/4/26. * 联系人实体类 * 功能:实现了People联系人实体类 */ public class myFile { public String PeopleName; public int ImgId; public String PeopleNumber; public myFile(String peopleName, int imgId, String peopleNumber) { super(); PeopleName = peopleName; ImgId = imgId; PeopleNumber = peopleNumber; } public myFile() { super(); } }
(3)联系人列表的适配器
/** * Created by monster on 2015/4/26. */ public class PeopleListAdapter extends CursorAdapter { private LayoutInflater mInflater;//定义LayoutInflater对象 public PeopleListAdapter(Context context, Cursor c) { super(context, c); mInflater=LayoutInflater.from(context); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { //初始化每一个item的View return mInflater.inflate(R.layout.activity_list_item,parent,false); } @Override public void bindView(View view, Context context, Cursor cursor) { //设置Item的布局效果 ViewHolder viewHolder=new ViewHolder(); viewHolder.PeopleImage=(ImageView)view.findViewById(R.id.IvNews); viewHolder.PeopleName=(TextView)view.findViewById(R.id.TvNewsTitle); viewHolder.PeopleNumber=(TextView)view.findViewById(R.id.TvNewsInfo); //从手机联系人中读取数据 viewHolder.PeopleImage.setBackgroundResource(R.mipmap.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))); } class ViewHolder{ ImageView PeopleImage; TextView PeopleName; TextView PeopleNumber; } }
(4)书写MainActivity.java
package cn.edu.bzu.contacts_listview; 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 { private ListView Lv; private PeopleListAdapter adapter; private Cursor cursor;//定义用来存储需要显示的对象的cursor @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView();//控件实例化 setData();//设置数据 setLv();//绑定适配器 } private void setLv() { adapter=new PeopleListAdapter(MainActivity.this,cursor); Lv.setAdapter(adapter); } private void setData() { //通过getContentResolver获取手机中联系人信息 cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null); } private void findView() { Lv=(ListView)findViewById(R.id.LV); } }
(5)在清单文件中设置权限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
----------------------------------------------------【总结部分】------------------------------------------------------
【总结】
适配器中继承的是CursorAdapter适配器:必须实现的三个方法为:
public PeopleListAdapter(Context context, Cursor c)
public View newView(Context context, Cursor cursor, ViewGroup parent) --->>初始化每一个item的View
public void bindView(View view, Context context, Cursor cursor) ---->>设置Item的布局效果
(读取手机联系人的数据)
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)) --->>读取联系人姓名的代码
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) --->>读取联系人号码的代码
【附录】
源代码地址:https://github.com/monsterLin/contacts_listview
标签:
原文地址:http://www.cnblogs.com/boy1025/p/4458559.html