当前位置: 移动技术网 > 移动技术>移动开发>Android > 简单的安卓通讯录实现教程

简单的安卓通讯录实现教程

2018年12月09日  | 移动技术网移动技术  | 我要评论

第一次demo安卓项目,理解适配器adapter的填充,sqlite。

(1)最重要的是适配器填充:

//得到手机通讯录联系人信息
getphonecontacts();
//对数据进行适配
mlistview =(listview)findviewbyid(r.id.lv);
myadapter = new mylistadapter(this);
mlistview.setadapter(myadapter);

获取手机联系人:

(首先获取resolver,通过resolver.query方法执行,执行结果给cuser光标,使用movetotest方法扫描结果,cuser.getstring方法获取字段string类型,再放入arraylist,add方法添加.)

private void getphonecontacts() {
 contentresolver resolver = mcontext.getcontentresolver();
 // 获取手机联系人
 cursor phonecursor = resolver.query(phone.content_uri,phones_projection,null,null,null);
 if (phonecursor != null) {
  while (phonecursor.movetonext()) {
//得到手机号码
string phonenumber = phonecursor.getstring(phones_number_index);
//当手机号码为空的或者为空字段 跳过当前循环
if (textutils.isempty(phonenumber))
 continue;
//得到联系人名称
string contactname = phonecursor.getstring(phones_display_name_index);
//得到联系人id
long contactid = phonecursor.getlong(phones_contact_id_index);
//得到联系人头像id
long photoid = phonecursor.getlong(phones_photo_id_index);
//得到联系人头像bitamp
bitmap contactphoto = null;
//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if(photoid > 0 ) {
 uri uri =contenturis.withappendedid(contactscontract.contacts.content_uri,contactid);
 inputstream input = contactscontract.contacts.opencontactphotoinputstream(resolver, uri);
 contactphoto = bitmapfactory.decodestream(input);
}else {
 contactphoto = bitmapfactory.decoderesource(getresources(), r.drawable.ic_launcher);
}
mcontactsname.add(contactname);
mcontactsnumber.add(phonenumber);
mcontactsphonto.add(contactphoto);
  }
  phonecursor.close();
 }
}

适配器myadapter:

(用convertview加载布局,intent调用功能,startactivity(intent)启动,返回加载好的convertview)

//定义一个适配器mylistadapter对联系人列表中的数据进行适配
class mylistadapter extends baseadapter {

 public mylistadapter(context context) {
  mcontext = context;
 }

 public int getcount() {
  //设置返回联系人条目的数量
  return mcontactsname.size();
 }

 public boolean areallitemsenabled() {
  return false;
 }

 public object getitem(int position) {
  return position;
 }

 public long getitemid(int position) {
  return position;
 }

 public view getview(final int position, view convertview, viewgroup parent) {
  imageview iamge = null;
  textview title = null;
  textview text = null;
  imageview image=null;
  imagebutton phone = null;
  imagebutton message =null;
  //convertview这个参数用于之前加载好的布局进行缓存,以便之后可以进行重用
  if (convertview == null) {
convertview = layoutinflater.from(mcontext).inflate(r.layout.item, null);
  }//拨打电话图标和发送短信图标则直接在布局中加载出来了

  iamge = (imageview) convertview.findviewbyid(r.id.imageview1);
  title = (textview) convertview.findviewbyid(r.id.tv_name);
  text = (textview) convertview.findviewbyid(r.id.tv_number);
  image = (imageview)convertview.findviewbyid(r.id.imageview1);
  phone = (imagebutton) convertview.findviewbyid(r.id.imagebutton1);
  message = (imagebutton) convertview.findviewbyid(r.id.imagebutton2);

  //绘制联系人名称
  title.settext(mcontactsname.get(position));
  //绘制联系人号码
  text.settext(mcontactsnumber.get(position));
  //绘制联系人头像
  iamge.setimagebitmap(mcontactsphonto.get(position));

  //点击拨打电话图标
  phone.setonclicklistener(new onclicklistener(){
public void onclick(view v){
 //用 intent 启动拨打电话(根据联系人的电话号码)
 intent intent = new intent(intent.action_call,uri.parse("tel:"+ mcontactsnumber.get(position)));
 startactivity(intent);
}
  });


  //点击发送短信图标
  message.setonclicklistener(new onclicklistener(){
public void onclick(view v){
 //用 intent 启动拨发短信
 dosendsmsto( mcontactsnumber.get(position),"哈哈!~~");
}

  });

  return convertview;
 }

按钮要设置setonclicklistener事件监听器,随时监控。

actiyity要写在清单文件里


uri是标识符,position是为位置。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网