Android中 自定义数据绑定适配器BaseAdapter的方法
时间:2022-02-02 21:12:50
public class PersonAdapter extends BaseAdapter {
private List persons;// 要绑定的数据
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() {// 得到要绑定的数据总数
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;
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java毕业设计实战之在线蛋糕销售商城的实现
![](https://img.aspxhome.com/file/2023/5/71815_0s.png)
SpringCloud URL重定向及转发代码实例
Android 后台发送邮件示例 (收集应用异常信息+Demo代码)
Java如何根据不同系统动态获取换行符和盘分割符
C#基础之异步调用实例教程
详解spring boot实现多数据源代码实战
SpringBoot关于自定义注解实现接口幂等性方式
![](https://img.aspxhome.com/file/2023/2/77982_0s.png)
Android模仿美团顶部的滑动菜单实例代码
![](https://img.aspxhome.com/file/2023/5/137115_0s.gif)
Android ViewPager实现Banner循环播放
![](https://img.aspxhome.com/file/2023/9/93259_0s.jpg)
Java轻量级权限认证框架Sa-Token的使用
![](https://img.aspxhome.com/file/2023/8/105948_0s.jpg)
Android自定义控件实现圆形进度条
![](https://img.aspxhome.com/file/2023/1/129151_0s.jpg)
java web实现简单留言板功能
剖析设计模式编程中C#对于组合模式的运用
![](https://img.aspxhome.com/file/2023/4/100164_0s.png)
C#如何从byte[]中直接读取Structure实例详解
![](https://img.aspxhome.com/file/2023/7/100207_0s.png)
SpringMVC Restful api接口实现的代码
![](https://img.aspxhome.com/file/2023/8/60528_0s.jpg)
通过实例了解Java 8创建Stream流的5种方法
![](https://img.aspxhome.com/file/2023/8/77398_0s.jpg)
MyBatis找不到mapper文件的实现
![](https://img.aspxhome.com/file/2023/1/61131_0s.png)
C#中的随机数函数Random()
Android View移动的六种方法小结
maven profile动态选择配置文件详解
![](https://img.aspxhome.com/file/2023/4/64514_0s.png)