源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-09-16 21:46 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android中 自定义数据绑定适配器BaseAdapter的方法
[u]复制代码[/u] 代码如下:
public class PersonAdapter extends[b]BaseAdapter[/b] {  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;  } }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部