[coolxing按: 转载请注明作者和出处, 如有谬误, 欢迎在评论中指正.]
最近在做一个天气预报的小课题, 其中涉及到了ListView的动态刷新, 以及如何在ListView上显示图片.
1. 在ListView上显示图片.
为了实现这个功能, 首先需要定义一个布局文件, 用于显示ListView的每个Item. 比如list_item.xml:
接下来需要选择一个合适的adapter, 由于系统提供的adapter都无法满足显示图片的需求(这点我并不确定), 因此在这里我自定义了一个BaseAdapter的子类:
public class MyListAdapter extends BaseAdapter {
private Activity context;
private List list;
public MyListAdapter(Activity context, List list) {
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View itemView = inflater.inflate(R.layout.list_item, null);
WeatherInfomation info = list.get(position);
TextView textView = (TextView) itemView.findViewById(R.id.list_text);
ImageView imageView = (ImageView) itemView
.findViewById(R.id.list_image);
textView.setText(info.getWeatherText());
imageView.setImageBitmap(info.getWeatherBitmap());
return itemView;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
可以看到, 定义BaseAdapter的子类需要重写很多方法, 其他方法都不难, 关键就是getView()方法. getView()方法返回的itemView对象用来显示ListView的一个Item. 使用LayoutInflater类的inflater方法可以从ist_item.xml文件中解析出itemView对象, 但此时不可直接将itemView返回, 还需定义itemView中的ImageView和TextView的行为. 可以将itemView理解为ImageView和TextView的父控件. 源文件中涉及的WeatherInfomation是我定义的一个实体类, 代码如下:
public class WeatherInfomation {
/**
* 代表天气状况的图片
*/
private Bitmap weatherBitmap;
/**
* 具体天气的文字说明
*/
private String weatherText;
public WeatherInfomation(Bitmap weatherBitmap, String weatherText) {
super();
this.weatherBitmap = weatherBitmap;
this.weatherText = weatherText;
}
public Bitmap getWeatherBitmap() {
return weatherBitmap;
}
public void setWeatherBitmap(Bitmap weatherBitmap) {
this.weatherBitmap = weatherBitmap;
}
public String getWeatherText() {
return weatherText;
}
public void setWeatherText(String weatherText) {
this.weatherText = weatherText;
}
}
接下来只需要创建出MyListAdapter对象并调用listview的setAdapter()方法就可以:
infomations = new ArrayList();
myListAdapter = new MyListAdapter(MainActivity.this, infomations);
listView.setAdapter(myListAdapter);
这样就可以在listview上显示图片了.
2. 很多朋友都知道, 想要动态刷新listview中的内容, 只要调用在数据发生改变之后调用adapter的notifyDataSetChanged()方法就可以了. 这里需要注意的是所谓的"数据发生改变"到底指的是什么, 比如说如下的代码:
infomations = new ArrayList();
infomations.add(new WeatherInfomation());
此时是否满足发生了数据改变的情形? 其实是没有的!(这个问题导致我浪费了很长时间, 我怎么花了一天时间才找出无法动态刷新listview的原因, 希望可以让看到的朋友避免这个问题). adapter在android中属于MVC中的mode, 它是UI界面和数据之间的桥梁, 而此时的数据, 并不是只infomations, 而是infomations这个引用所指向的WeatherInfomation集合, 因此重新创建一个 ArrayList
而adapter关联的数据并没有发生变化. 后来我的处理代码是:
infomations.clear();
infomations.add(new WeatherInfomation());
这样就真正改变了adapter所关联的数据了.