欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android BaseAdapter应用实例

程序员文章站 2023-12-20 19:24:40
本文实例讲述了android baseadapter的应用方法。分享给大家供大家参考,具体如下: 直接上源码 /** * 生成联系人items,即items中...

本文实例讲述了android baseadapter的应用方法。分享给大家供大家参考,具体如下:

直接上源码

/**
* 生成联系人items,即items中包含如下控件
*
*/
public final class viewholder{
  public imageview callimg;//打电话的图标
  public imageview headimg;//头像
  public textview peoplename;//联系人名
  public textview peoplenumber;//联系人号码
}
public class myadapter extends baseadapter {
 private layoutinflater minflater;
 public myadapter(context context) {
  this.minflater = layoutinflater.from(context);
 }
 public int getcount() {
   return listpeople.size();
 }
 public object getitem(int arg0) {
   return listpeople.get(arg0);
 }
 public long getitemid(int arg0) {
  return arg0;//当初在看别人例子时,此处返回null,加载都没问题,但在listview中的onitemsclick事件中就报错了。
 }
 public view getview(final int position, view convertview,
  viewgroup parent) {
  viewholder holder = null;
  if (convertview == null) {
  holder = new viewholder();
  convertview = minflater.inflate(r.layout.peopleinfo, null);
  holder.callimg = (imageview) convertview
   .findviewbyid(r.id.callbtn);
  holder.headimg = (imageview) convertview
   .findviewbyid(r.id.contactimage);
  holder.peoplename = (textview) convertview
   .findviewbyid(r.id.peoplename);
  holder.peoplenumber = (textview) convertview
   .findviewbyid(r.id.peoplenumber);
  convertview.settag(holder);
  } else {
  holder = (viewholder) convertview.gettag();
  }
  final string name = (string) listpeople.get(position).get(
   "peoplename");
  final string num = (string) listpeople.get(position).get(
   "peoplenumber");
  holder.peoplename.settext((string) listpeople.get(position).get(
   "peoplename"));
  holder.peoplenumber.settext((string) listpeople.get(position).get(
   "peoplenumber"));
  //  holder.headimg.setimageresource(r.drawable.ic_menu_contact);
  if(listpeople.get(position).get("peoplephoto") != null){
  holder.headimg.setimagebitmap((bitmap)listpeople.get(position).get(
  "peoplephoto"));
  }
  holder.callimg.setimageresource(r.drawable.ic_dial_action_call);
  holder.callimg.setonclicklistener(new view.onclicklistener() {
  public void onclick(view v) {
  //处理items中控件的触发事件
  }
  });
  return convertview;
 }
 }
/*
*应用baseadapter
*
*/
final myadapter myadapter = new myadapter(this);
  contacts.setadapter(myadapter);
  contacts.setonitemclicklistener(new onitemclicklistener() {
  @suppresswarnings("unchecked")
  public void onitemclick(adapterview<?> arg0, view arg1, int arg2,
   long arg3) {
   map<string, object> map = (map<string, object>) myadapter.getitem(arg2);//此处需要注意,
   string peoplenumber = string.valueof(map.get("peoplenumber"));
  }
});

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: