在使用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;
}
}