Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
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;
}
}
- Android DataBinding單向數(shù)據(jù)綁定深入探究
- 淺析Android企業(yè)級(jí)開(kāi)發(fā)數(shù)據(jù)綁定技術(shù)
- Android Studio綁定下拉框數(shù)據(jù)詳解
- 詳解Android的MVVM框架 - 數(shù)據(jù)綁定
- Android Data Binding數(shù)據(jù)綁定詳解
- Android RecyclerView 數(shù)據(jù)綁定實(shí)例代碼
- Android ListView數(shù)據(jù)綁定顯示的三種解決方法
- Android數(shù)據(jù)雙向綁定原理實(shí)現(xiàn)和應(yīng)用場(chǎng)景
相關(guān)文章
詳解android studio游戲搖桿開(kāi)發(fā)教程,仿王者榮耀搖桿
這篇文章主要介紹了android studio游戲搖桿開(kāi)發(fā)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Android繪制旋轉(zhuǎn)動(dòng)畫(huà)方法詳解
這篇文章主要介紹了Android如何采用RotateAnimation繪制一個(gè)旋轉(zhuǎn)動(dòng)畫(huà),文中的實(shí)現(xiàn)方法講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試2022-01-01Android進(jìn)度條ProgressBar的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android進(jìn)度條ProgressBar的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09android調(diào)用web service(cxf)實(shí)例應(yīng)用詳解
Google為ndroid平臺(tái)開(kāi)發(fā)Web Service提供了支持,提供了Ksoap2-android相關(guān)架包接下來(lái)介紹android調(diào)用web service(cxf),感興趣的朋友可以了解下2013-01-01Android scrollToTop實(shí)現(xiàn)點(diǎn)擊回到頂部(兼容PullTorefreshScrollview)
當(dāng)頁(yè)面滑動(dòng)到底部,出現(xiàn)回到頂部的按鈕相信對(duì)大家來(lái)說(shuō)并不陌生,下面這篇文章主要介紹了關(guān)于Android scrollToTop實(shí)現(xiàn)點(diǎn)擊回到頂部,并兼容PullTorefreshScrollview的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒。2017-03-03Android手機(jī)(設(shè)備)連接掃描槍掃碼遇到的問(wèn)題
這篇文章給大家分享了Android手機(jī)(設(shè)備)連接掃描槍掃碼遇到的問(wèn)題以及解決辦法,有需要的參考下。2018-07-07Android7.0實(shí)現(xiàn)拍照和相冊(cè)選取圖片功能
這篇文章主要為大家詳細(xì)介紹了Android7.0實(shí)現(xiàn)拍照和相冊(cè)選取圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07