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;
}
}
新聞熱點
疑難解答
圖片精選