在使用listview中需要使用到adapter类。adapter类的作用是嫁接数据和view的一个桥梁。ArrayAdapter是一个android实现好的类,感觉比自己写强多了,它实现了add、insert等修改数据的方法,在这些方法内部直接就调用了 notifyDataSetChanged方法,省去了自己编写adapter中忘记调用这个方法,造成view无法更新数据。
public class MyAdaptor extends ArrayAdapter<Bean> {
LayoutInflater inflater ;
List<Bean> mObjects ;
public MyAdaptor(Context context, int resource, List<Bean> objects) {
super(context, resource, objects);
inflater = LayoutInflater.from(context);
mObjects =objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if(convertView != null){
view = convertView;
}else {
view = inflater.inflate(R.layout.list_item,parent,false);
}
//设置view的
ImageView iv = (ImageView) view.findViewById(R.id.iv);
TextView name= (TextView)view.findViewById(R.id.name);
TextView time = (TextView)view.findViewById(R.id.time);
iv.setImageBitmap(mObjects.get(position).bitmap);
name.setText(mObjects.get(position).name);
time.setText(mObjects.get(position).time);
return view;
}
}