Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
public class PersonAdapter extends BaseAdapter {
private List persons;// 要綁定的數(shù)據(jù)
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() {// 得到要綁定的數(shù)據(jù)總數(shù)
return persons.size();
}
@Override
public Object getItem(int position) {// 給定索引值,得到索引值對應的對象
return persons.get(position);
}
@Override
public long getItemId(int position) {// 獲取條目id
return position;
}
// ListView有緩存功能,當顯示第一頁頁面時會創(chuàng)建頁面對象,顯示第二頁時重用第一頁創(chuàng)建好了的對象
// 取得條目界面:position代表當前條目所要綁定的數(shù)據(jù)在集合中的索引值
@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);
// 實現(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;
}
}
相關文章
詳解android studio游戲搖桿開發(fā)教程,仿王者榮耀搖桿
這篇文章主要介紹了android studio游戲搖桿開發(fā)教程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05Android進度條ProgressBar的實現(xiàn)代碼
這篇文章主要為大家詳細介紹了Android進度條ProgressBar的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09android調用web service(cxf)實例應用詳解
Google為ndroid平臺開發(fā)Web Service提供了支持,提供了Ksoap2-android相關架包接下來介紹android調用web service(cxf),感興趣的朋友可以了解下2013-01-01Android scrollToTop實現(xiàn)點擊回到頂部(兼容PullTorefreshScrollview)
當頁面滑動到底部,出現(xiàn)回到頂部的按鈕相信對大家來說并不陌生,下面這篇文章主要介紹了關于Android scrollToTop實現(xiàn)點擊回到頂部,并兼容PullTorefreshScrollview的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒。2017-03-03