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;
}
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注