[coolxing按: 转载请注明作者和出处, 如有谬误, 欢迎在评论中指正.]
最近在做一个天气预报的小课题, 其中涉及到了ListView的动态刷新, 以及如何在ListView上显示图片.
1. 在ListView上显示图片.
为了实现这个功能, 首先需要定义一个布局文件, 用于显示ListView的每个Item. 比如list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/list_image"
android:layout_marginTop="6px" android:layout_marginBottom="6px"
android:layout_marginLeft="6px" android:layout_marginRight="6px"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/list_text"
android:layout_marginLeft="6px" android:layout_marginRight="6px" android:layout_gravity="center_vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
接下来需要选择一个合适的adapter, 由于系统提供的adapter都无法满足显示图片的需求(这点我并不确定), 因此在这里我自定义了一个BaseAdapter的子类:
public class MyListAdapter extends BaseAdapter {
private Activity context;
private List<WeatherInfomation> list;
public MyListAdapter(Activity context, List<WeatherInfomation> 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<WeatherInfomation>();
myListAdapter = new MyListAdapter(MainActivity.this, infomations);
listView.setAdapter(myListAdapter);
这样就可以在listview上显示图片了.
2. 很多朋友都知道, 想要动态刷新listview中的内容, 只要调用在数据发生改变之后调用adapter的notifyDataSetChanged()方法就可以了. 这里需要注意的是所谓的"数据发生改变"到底指的是什么, 比如说如下的代码:
infomations = new ArrayList<WeatherInfomation>();
infomations.add(new WeatherInfomation());
此时是否满足发生了数据改变的情形? 其实是没有的!(这个问题导致我浪费了很长时间, 我怎么花了一天时间才找出无法动态刷新listview的原因, 希望可以让看到的朋友避免这个问题). adapter在android中属于MVC中的mode, 它是UI界面和数据之间的桥梁, 而此时的数据, 并不是只infomations, 而是infomations这个引用所指向的WeatherInfomation集合, 因此重新创建一个 ArrayList<WeatherInfomation>并将其赋值给infomations改变的只是infomations的值,
而adapter关联的数据并没有发生变化. 后来我的处理代码是:
infomations.clear();
infomations.add(new WeatherInfomation());
这样就真正改变了adapter所关联的数据了.
分享到:
相关推荐
Android开发笔记---ListView
android-pulltorefresh-listview 下拉刷新ListView
Android自学笔记-11-listview简单例子
android--滑动删除的ListView
blog地址:http://www.cnblogs.com/ityizhainan/p/6273919.html
android-world-of-listview-android
Android学习笔记-采用ListView实现数据列表显示-以及各种适配器使用-和如何写.pdf
ListView下拉刷新 ListView下拉刷新ListView下拉刷新ListView下拉刷新
Android_ListView动态刷新
For more details, visitAnimated-Expanding-ListView Developed by @LeonardoCardoso. Animated Expanding ListView provides a fancy animation on expanding or collapsing the content of a listview item. It...
2-android-pulltorefresh-listview 下拉刷新ListView.zip
整理了下以前写的小项目,ListView的下拉刷新,虽然小但还是想纪念下。。适合新手看,大神略过。。。
Android中ListView用NotifyDataSetChanged实现添加数据时动态刷新
android解析网络图片并显示在listview
Android -- ListView 返回 第一条数据的实现.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android-react-native-timeline-listview.zip,React本机应用程序的时间线组件,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
android 实现listview动态下拉刷新,动态加载数据 可以避免一次数据加载过多
Android高级应用源码-仿网易新闻listview加header图片滚动,上拉下拉刷新.zip
滑动listview标题置顶,listview吸顶效果,安卓系统状态栏透明,有图有源码
android--ListView实现带标题栏效果(附源码)