把数据弄得适当,适合以在于View上显示
我们常用的适配器一共有三个,当然不包含自定义的适配器,
那就是ArrayAdapter,SimpleAdapter,SimpleCursorAdapter这三个,他们都是继BaseAdapter
。
ArrayAdapter最为简单,只能展示一行字。
SimpleAdapter有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合。
四个方法:
public int getCount() //得到数据的行数
public Object getItem(int position)//根据position得到某一行的记录
public long getItemId(int position)//得到某一条记录的ID
public View getView(int position, View convertView, ViewGroup parent)
BaseAdapter例子:
public class MyAdapter extends BaseAdapter {
private List<ItemBean> list;
private LayoutInflater inflater;
public MyAdapter(Context context, List<ItemBean> list) {
inflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// // 逗比式
// View view = inflater.inflate(R.layout.item, null);
// ImageView imageview = (ImageView) view.findViewById(R.id.imageview);
// TextView tittle = (TextView) view.findViewById(R.id.tittle);
// TextView context = (TextView) view.findViewById(R.id.context);
// ItemBean bean = list.get(position);
// imageview.setImageResource(bean.ImageResid);
// tittle.setText(bean.title);
// context.setText(bean.context);
// return view;
// // 普通式,利用了ListView的缓存,使用convertView
// if (convertView == null) {
// convertView = inflater.inflate(R.layout.item, null);
// }
// ImageView imageview = (ImageView) convertView
// .findViewById(R.id.imageview);
// TextView tittle = (TextView) convertView.findViewById(R.id.tittle);
// TextView context = (TextView) convertView.findViewById(R.id.context);
// ItemBean bean = list.get(position);
// imageview.setImageResource(bean.ImageResid);
// tittle.setText(bean.title);
// context.setText(bean.context);
// return convertView;
// 文艺式,利用了ListView的缓存,更通过ViewHolder类来实现显示数据的试图的缓存,避免多次通过findViewById寻找控件
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.item, null);
viewHolder.ViewHolderimageView = (ImageView) convertView
.findViewById(R.id.imageview);
viewHolder.ViewHoldertitle = (TextView) convertView
.findViewById(R.id.tittle);
viewHolder.ViewHoldercontent = (TextView) convertView
.findViewById(R.id.context);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ItemBean bean = list.get(position);
viewHolder.ViewHolderimageView.setImageResource(bean.ImageResid);
viewHolder.ViewHoldertitle.setText(bean.title);
viewHolder.ViewHoldercontent.setText(bean.context);
return convertView;
}
class ViewHolder {
public ImageView ViewHolderimageView;
public TextView ViewHoldertitle;
public TextView ViewHoldercontent;
}
}
我们常用的适配器一共有三个,当然不包含自定义的适配器,
那就是ArrayAdapter,SimpleAdapter,SimpleCursorAdapter这三个,他们都是继BaseAdapter
。
ArrayAdapter最为简单,只能展示一行字。
SimpleAdapter有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合。
四个方法:
public int getCount() //得到数据的行数
public Object getItem(int position)//根据position得到某一行的记录
public long getItemId(int position)//得到某一条记录的ID
public View getView(int position, View convertView, ViewGroup parent)
BaseAdapter例子:
public class MyAdapter extends BaseAdapter {
private List<ItemBean> list;
private LayoutInflater inflater;
public MyAdapter(Context context, List<ItemBean> list) {
inflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// // 逗比式
// View view = inflater.inflate(R.layout.item, null);
// ImageView imageview = (ImageView) view.findViewById(R.id.imageview);
// TextView tittle = (TextView) view.findViewById(R.id.tittle);
// TextView context = (TextView) view.findViewById(R.id.context);
// ItemBean bean = list.get(position);
// imageview.setImageResource(bean.ImageResid);
// tittle.setText(bean.title);
// context.setText(bean.context);
// return view;
// // 普通式,利用了ListView的缓存,使用convertView
// if (convertView == null) {
// convertView = inflater.inflate(R.layout.item, null);
// }
// ImageView imageview = (ImageView) convertView
// .findViewById(R.id.imageview);
// TextView tittle = (TextView) convertView.findViewById(R.id.tittle);
// TextView context = (TextView) convertView.findViewById(R.id.context);
// ItemBean bean = list.get(position);
// imageview.setImageResource(bean.ImageResid);
// tittle.setText(bean.title);
// context.setText(bean.context);
// return convertView;
// 文艺式,利用了ListView的缓存,更通过ViewHolder类来实现显示数据的试图的缓存,避免多次通过findViewById寻找控件
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.item, null);
viewHolder.ViewHolderimageView = (ImageView) convertView
.findViewById(R.id.imageview);
viewHolder.ViewHoldertitle = (TextView) convertView
.findViewById(R.id.tittle);
viewHolder.ViewHoldercontent = (TextView) convertView
.findViewById(R.id.context);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ItemBean bean = list.get(position);
viewHolder.ViewHolderimageView.setImageResource(bean.ImageResid);
viewHolder.ViewHoldertitle.setText(bean.title);
viewHolder.ViewHoldercontent.setText(bean.context);
return convertView;
}
class ViewHolder {
public ImageView ViewHolderimageView;
public TextView ViewHoldertitle;
public TextView ViewHoldercontent;
}
}