Android中 自定义数据绑定适配器BaseAdapter的方法

所属分类: 软件编程 / Android 阅读数: 1187
收藏 0 赞 0 分享

复制代码 代码如下:

public class PersonAdapter extends BaseAdapter {
 private List persons;// 要绑定的数据
 private int resource;// 绑定的一个条目界面的id,此例中即为item.xml
 private LayoutInflater inflater;// 布局填充器,它可以使用一个xml文件生成一个View对象,可以通过Context获取实例对象

 public PersonAdapter(Context context, List persons, int resource) {
  inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  this.resource = resource;
  this.persons = persons;
 }

 @Override
 public int getCount() {// 得到要绑定的数据总数
  return persons.size();
 }

 @Override
 public Object getItem(int position) {// 给定索引值,得到索引值对应的对象
  return persons.get(position);
 }

 @Override
 public long getItemId(int position) {// 获取条目id
  return position;
 }

 // ListView有缓存功能,当显示第一页页面时会创建页面对象,显示第二页时重用第一页创建好了的对象
 // 取得条目界面:position代表当前条目所要绑定的数据在集合中的索引值
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  TextView nameView = null;
  TextView phoneView = null;
  TextView amountView = null;
  if (convertView == null) {// 显示第一页的时候convertView为空
   convertView = inflater.inflate(resource, null);// 生成条目对象
   nameView = (TextView) convertView.findViewById(R.id.name);
   phoneView = (TextView) convertView.findViewById(R.id.phone);
   amountView = (TextView) convertView.findViewById(R.id.amount);

   ViewCache cache = new ViewCache();
   cache.amountView = amountView;
   cache.nameView = nameView;
   cache.phoneView = phoneView;
   convertView.setTag(cache);
  } else {
   ViewCache cache = (ViewCache) convertView.getTag();
   amountView = cache.amountView;
   nameView = cache.nameView;
   phoneView = cache.phoneView;
  }

  Person person = persons.get(position);
  // 实现数据绑定
  nameView.setText(person.getName());
  phoneView.setText(person.getPhone());
  amountView.setText(person.getAmount());
  return convertView;
 }

 private final class ViewCache {
  public TextView nameView;
  public TextView phoneView;
  public TextView amountView;
 }
}

更多精彩内容其他人还在看

Android 动画之AlphaAnimation应用详解

本节讲解AlphaAnimation 动画,窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation,具体的祥看本文,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之TranslateAnimation应用详解

本节讲解TranslateAnimation动画,TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现,本文将详细介绍通过TranslateAnimation 来定义动画,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之ScaleAnimation应用详解

本节讲解ScaleAnimation 动画在应用中的实现,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之RotateAnimation应用详解

本节讲解旋转动画效果RotateAnimation方法的应用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之文件操作模式深入理解

本文将介绍Android开发之文件操作模式,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android应用程序窗口(Activity)窗口对象(Window)创建指南

本文将详细介绍Android应用程序窗口(Activity)的窗口对象(Window)的创建过程,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android activity设置无标题实现全屏

本文将详细介绍Android如何设置Activity全屏和无标题的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android启动模拟器报错解决方法

本文将详细介绍Android模拟器报"Failed To Allocate memory 8"错误的解决办法,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android如何实现非本地图片的点击态

Android如何实现非本地图片的点击态,本文提供了详细的实现代码,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android viewpaper实例探讨

本文将提供一个android viewpaper实例实现过程,需要了解更多的朋友可以参考下
收藏 0 赞 0 分享
查看更多