android_listView_适配器Adapter的使用
2013-03-06 12:40
323 查看
1.简单适配器
2.结果集适配器
3.自定义适配器
自定义适配器中的PersonAdapetr
private void show() { List<Person> persons = personService.getScrollData(0, 20);//listView存放的数据 List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>(); for(Person person : persons){ HashMap<String, Object> item = new HashMap<String, Object>(); item.put("name", person.getName()); item.put("phone", person.getPhone()); item.put("amount", person.getAmount()); item.put("id", person.getId()); data.add(item); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount}); listView.setAdapter(adapter); }
2.结果集适配器
public class PersonAdapter extends BaseAdapter { private List<Person> persons;//在绑定的数据 private int resource;//绑定的条目界面 private LayoutInflater inflater; }
private void show2() { Cursor cursor = personService.getCursorScrollData(0, 20);//listView存放的数据 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount}); listView.setAdapter(adapter); }
3.自定义适配器
//自定义适配器 private void show3() { List<Person> persons = personService.getScrollData(0, 20);//listView存放的数据 PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item); listView.setAdapter(adapter); }
自定义适配器中的PersonAdapetr
public PersonAdapter(Context context, List<Person> persons, int resource) { this.persons = persons;//数据源,listView要放的数据 this.resource = resource;//每个条目的View的界面id inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return persons.size();//数据总数 } @Override public Object getItem(int position) { return persons.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView nameView = null; TextView phoneView = null; TextView amountView = null; if(convertView==null){//条目重用 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.nameView = nameView; cache.phoneView = phoneView; cache.amountView = amountView; convertView.setTag(cache); }else{ ViewCache cache = (ViewCache) convertView.getTag(); nameView = cache.nameView; phoneView = cache.phoneView; amountView = cache.amountView; } Person person = persons.get(position); //下面代码实现数据绑定 nameView.setText(person.getName()); phoneView.setText(person.getPhone()); amountView.setText(person.getAmount().toString()); return convertView; } private final class ViewCache{ public TextView nameView; public TextView phoneView; public TextView amountView; } }
相关文章推荐
- Android ListView适配器Adapter的使用,item的复用和ViewHolder优化
- android之ListView中使用的适配器CursorAdapter
- Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配器)--《第一行代码Android》学习笔记
- Android中ListView 控件与 Adapter 适配器如何使用?
- Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配器)--《第一行代码Android》学习笔记
- Android-ListView适配器BaseAdapter的使用和优化
- Android中ListView常用的两种适配器ArrayAdapter和SimpleAdapter使用
- Android中ListView,Gallery,GridView 的适配器Adapter的使用
- Android最简单的使用数组的适配器Adapter
- Android 之 ListView使用SimpleAdapter展示列表
- Android ListView 中Adapter的使用及listView的动态加载
- Android实例demo11之listview的简单使用(arrayadapter、simpleadapter)
- Android ListView控件的使用(自定义适配器 + ListView + 解决OOM )
- Android 基本控件使用六(ListView+SimpleAdapter)
- Android -- listview的使用, Inflater打气筒创建View对象,三种数据Adapter
- Android自定义ListView或GridView适配器Adapter的getView方法不执行的问题
- Android ListView-各种Adapter配合使用
- Android异步下载网络图片&android解析xml文件的方式&使用Adapter为ListView提供数据
- Android_ListView_Adapter使用和数据动态加载
- android ListView之BaseAdapter的使用方式