欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法

 更新時(shí)間:2013年04月21日 14:11:07   作者:  
本篇文章小編為大家介紹,Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法。需要的朋友參考下

復(fù)制代碼 代碼如下:

public class PersonAdapter extends BaseAdapter {
 private List persons;// 要綁定的數(shù)據(jù)
 private int resource;// 綁定的一個(gè)條目界面的id,此例中即為item.xml
 private LayoutInflater inflater;// 布局填充器,它可以使用一個(gè)xml文件生成一個(gè)View對(duì)象,可以通過(guò)Context獲取實(shí)例對(duì)象

 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() {// 得到要綁定的數(shù)據(jù)總數(shù)
  return persons.size();
 }

 @Override
 public Object getItem(int position) {// 給定索引值,得到索引值對(duì)應(yīng)的對(duì)象
  return persons.get(position);
 }

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

 // ListView有緩存功能,當(dāng)顯示第一頁(yè)頁(yè)面時(shí)會(huì)創(chuàng)建頁(yè)面對(duì)象,顯示第二頁(yè)時(shí)重用第一頁(yè)創(chuàng)建好了的對(duì)象
 // 取得條目界面:position代表當(dāng)前條目所要綁定的數(shù)據(jù)在集合中的索引值
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  TextView nameView = null;
  TextView phoneView = null;
  TextView amountView = null;
  if (convertView == null) {// 顯示第一頁(yè)的時(shí)候convertView為空
   convertView = inflater.inflate(resource, null);// 生成條目對(duì)象
   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);
  // 實(shí)現(xiàn)數(shù)據(jù)綁定
  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;
 }
}

相關(guān)文章

最新評(píng)論