package com.test.main;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class WeatherAdapter extends BaseAdapter{
//数据源
private List<HashMap<String,String>> list;
private Context context;
private int []type;
//构造函数
public WeatherAdapter (Context context,List<HashMap<String,String>> list, int[] type){
this.context = context;
this.list = list;
this.type=type;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater mInflater = LayoutInflater.from(context);
//产生一个View
View view = null;
//根据type不同的数据类型构造不同的View,也可以根据1,2,3天数构造不同的样式
if(type[position]==0){
view = mInflater.inflate(R.layout.city_item, null);
//获取城市名称
String cityName=list.get(position).get("data");
ImageView image=(ImageView)view.findViewById(R.id.weather_image);
if(cityName.equals("北京")){
image.setImageResource(R.drawable.beijing);
}else if(cityName.equals("上海")){
image.setImageResource(R.drawable.shanghai);
}else if(cityName.equals("广州")){
image.setImageResource(R.drawable.guangzhou);
}else if(cityName.equals("深圳")){
image.setImageResource(R.drawable.shenzhen);
}
TextView city=(TextView)view.findViewById(R.id.city);
city.setText(cityName);
}else{
view = mInflater.inflate(R.layout.content_item, null);
//获取数据
String content=list.get(position).get("data");
//分离数据
String []items=content.split(",");
TextView weather=(TextView)view.findViewById(R.id.content);
weather.setText(items[0]+"天气: "+items[1]+";温度: "+items[2]);
TextView date=(TextView)view.findViewById(R.id.date);
date.setText(items[3]);
}
return view;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
不同样式Item的ListView 实现思路:定义Listview不同的背景,首先要定义好标题与表格内容的两个不同的xml布局文件(city_item.xml,content_item.xml)。在填充数据的时候往往采用构造一个Adapter数据类型,根据构造的数据类型,判断不同的数据类型,采用LayoutInflater类填充不同的layout文件,从而返回不同的View。
资源推荐
资源详情
资源评论
收起资源包目录
ListView.zip (34个子文件)
ListView
bin
classes.dex 7KB
com
test
main
WeatherAdapter.class 3KB
R$layout.class 448B
R$id.class 575B
R$drawable.class 576B
R$attr.class 325B
ListViewActivity.class 2KB
R.class 539B
R$string.class 412B
R$color.class 533B
resources.ap_ 26KB
ListView.apk 32KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
shenzhen.gif 3KB
listview_bg.xml 846B
guangzhou.gif 3KB
icon.png 4KB
beijing.gif 3KB
shanghai.gif 3KB
more.png 714B
values
strings.xml 175B
color.xml 300B
drawable-mdpi
icon.png 3KB
layout
content_item.xml 1KB
city_item.xml 957B
main.xml 505B
assets
default.properties 362B
gen
com
test
main
R.java 2KB
proguard.cfg 1KB
src
com
test
main
ListViewActivity.java 2KB
WeatherAdapter.java 2KB
.project 844B
.classpath 280B
AndroidManifest.xml 683B
共 34 条
- 1
资源评论
- vjligi2014-04-16额,这个完全不是我要找的
- zml50662013-10-14不错,可以参考学习
sichard
- 粉丝: 41
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功