android笔记--在ListView中显示图片及ListView的动态刷新
来源:互联网 发布:精易编程助手 下载 编辑:程序博客网 时间:2024/06/06 00:45
最近在做一个天气预报的小课题, 其中涉及到了ListView的动态刷新, 以及如何在ListView上显示图片.
1. 在ListView上显示图片.
为了实现这个功能, 首先需要定义一个布局文件, 用于显示ListView的每个Item. 比如list_item.xml:
01
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
04
05
<
ImageView
android:id
=
"@+id/list_image"
06
android:layout_marginTop
=
"6px"
android:layout_marginBottom
=
"6px"
07
android:layout_marginLeft
=
"6px"
android:layout_marginRight
=
"6px"
08
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
09
<
TextView
android:id
=
"@+id/list_text"
10
android:layout_marginLeft
=
"6px"
android:layout_marginRight
=
"6px"
android:layout_gravity
=
"center_vertical"
11
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
12
13
</
LinearLayout
>
01
public
class
MyListAdapter
extends
BaseAdapter {
02
private
Activity context;
03
private
List<WeatherInfomation> list;
04
05
public
MyListAdapter(Activity context, List<WeatherInfomation> list) {
06
this
.context = context;
07
this
.list = list;
08
}
09
10
@Override
11
public
View getView(
int
position, View convertView, ViewGroup parent) {
12
LayoutInflater inflater = context.getLayoutInflater();
13
View itemView = inflater.inflate(R.layout.list_item,
null
);
14
WeatherInfomation info = list.get(position);
15
TextView textView = (TextView) itemView.findViewById(R.id.list_text);
16
ImageView imageView = (ImageView) itemView
17
.findViewById(R.id.list_image);
18
textView.setText(info.getWeatherText());
19
imageView.setImageBitmap(info.getWeatherBitmap());
20
return
itemView;
21
}
22
23
@Override
24
public
int
getCount() {
25
return
list.size();
26
}
27
28
@Override
29
public
Object getItem(
int
position) {
30
return
list.get(position);
31
}
32
33
@Override
34
public
long
getItemId(
int
position) {
35
return
position;
36
}
37
}
01
public
class
WeatherInfomation {
02
/**
03
* 代表天气状况的图片
04
*/
05
private
Bitmap weatherBitmap;
06
/**
07
* 具体天气的文字说明
08
*/
09
private
String weatherText;
10
11
public
WeatherInfomation(Bitmap weatherBitmap, String weatherText) {
12
super
();
13
this
.weatherBitmap = weatherBitmap;
14
this
.weatherText = weatherText;
15
}
16
17
public
Bitmap getWeatherBitmap() {
18
return
weatherBitmap;
19
}
20
21
public
void
setWeatherBitmap(Bitmap weatherBitmap) {
22
this
.weatherBitmap = weatherBitmap;
23
}
24
25
public
String getWeatherText() {
26
return
weatherText;
27
}
28
29
public
void
setWeatherText(String weatherText) {
30
this
.weatherText = weatherText;
31
}
32
33
}
1
infomations =
new
ArrayList<WeatherInfomation>();
2
myListAdapter =
new
MyListAdapter(MainActivity.
this
, infomations);
3
listView.setAdapter(myListAdapter);
2. 很多朋友都知道, 想要动态刷新listview中的内容, 只要调用在数据发生改变之后调用adapter的notifyDataSetChanged()方法就可以了. 这里需要注意的是所谓的"数据发生改变"到底指的是什么, 比如说如下的代码:
1
infomations =
new
ArrayList<WeatherInfomation>();
2
infomations.add(
new
WeatherInfomation());
而adapter关联的数据并没有发生变化. 后来我的处理代码是:
1
infomations.clear();
2
infomations.add(
new
WeatherInfomation());
转自:http://coolxing.iteye.com/blog/1207946
- android笔记--在ListView中显示图片及ListView的动态刷新
- 在ListView中显示图片及ListView的动态刷新
- Android 自定义ListView + Gallery 组合,在ListView的一个item中显示多张图片
- 如何动态刷新ListView的显示---notifyDataSetChanged
- Android基础篇之在ListView中显示网络图片
- Android基础篇之在ListView中显示网络图片
- Android在ListView中嵌套ListView 解决嵌套的listview显示不全的问题
- Android开发笔记之ListView实现网络图片的显示
- Android ListView显示图片
- Android ListView控件显示数据库中图片
- Android ListView控件显示数据库中图片
- android中ListView不显示图片
- Android ListView中图片的异步加载显示
- Android ListView中图片的异步加载显示
- Android ListView中图片的异步加载显示
- 优化Android ListView中图片的异步加载显示
- android中ListView嵌套GridView图片显示的问题
- Android listview图片刷新闪烁
- android 异步处理
- Struts2与Servlet的关系
- char * a, char ** a, char * a[], char a[][], char * a[][], char ** a[][], char * a [][][], and so on
- Qt多线程 .
- Android中ListView分页加载数据
- android笔记--在ListView中显示图片及ListView的动态刷新
- iOS多线程编程指南(三)Run Loop
- 开发者所需要知道的iOS7 SDK新特性
- 编程规范题目
- XML与数据库
- jsp表单验证大全
- Rossum--slam的后记
- 快速掌握一个语言最常用的50%
- 为何说棱镜计划会扼杀云计算