手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android中 自定义数据绑定适配器BaseAdapter的方法
Android中 自定义数据绑定适配器BaseAdapter的方法
摘要:复制代码代码如下:publicclassPersonAdapterextendsBaseAdapter{privateListpersons...

复制代码 代码如下:

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;

}

}

【Android中 自定义数据绑定适配器BaseAdapter的方法】相关文章:

Android中使用Gson解析JSON数据的两种方法

android之计时器(Chronometer)的使用以及常用的方法

Android实现自定义带文字和图片Button的方法

Android 完全退出应用程序的解决方法

Android 设置应用全屏的两种解决方法

Android不读入内存获取图像宽高信息的方法

Android自定义属性 format的深入解析

android动态布局之动态加入TextView和ListView的方法

android 自定义控件 自定义属性详细介绍

Android中3种图片压缩处理方法

精品推荐
分类导航