现在以一个Demo为模型,展现如图-1的所示的背景内容,分析Listview的相关使用方法。
图-1 天气列表显示的demo
实现思路:定义Listview不同的背景,首先要定义好标题与表格内容的两个不同的xml布局文件(city_item.xml,content_item.xml)。在填充数据的时候往往采用构造一个Adapter数据类型,根据构造的数据类型,判断不同的数据类型,采用LayoutInflater类填充不同的layout文件,从而返回不同的View。
LayoutInflater使用方法:
- LayoutInflater inflater = LayoutInflater.from(this);
- View view=inflater.inflate(R.layout.ID, null);
构造WeatherAdapter类继承BaseAdapter并实现相关的接口函数,实现对列表数据的填充。其中数据的结构比较关键,可以控制相关的标题和内容的关系。
ListViewActivity类则主要是实现并填充相应的数据,显示相关数据。
程序代码如下:
1.WeatherAdapter.java
- 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;
- }
- }
2.ListViewActivity.java
- package com.test.main;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- public class ListViewActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ListView list=(ListView) findViewById(R.id.list_items);
- ArrayList<HashMap<String, String>> listItem = new
- ArrayList<HashMap<String, String>>();
- //各个城市的天气
- String []data={"北京","今天,晴天,22℃,2011-6-28","明天,多云转阵雨,32~23℃,2011-6-29","后天,阴转多云,33~23℃,2011-6-30",
- "上海","今天,阵雨转雷阵雨,31~25℃,2011-6-28","明天,雷阵雨转阴,31~26℃,2011-6-29","后天,阵雨转多云,32~27℃,2011-6-30",
- "广州","今天,中雨转暴雨,32~25℃,2011-6-28","明天,暴雨转大到暴雨,29~25℃,2011-6-29","后天,暴雨转阵雨,29~25℃,2011-6-30",
- "深圳","今天,中雨转暴雨,31~25℃,2011-6-28","明天,暴雨,29~24℃,2011-6-29","后天,大雨转阵雨,28~25℃,2011-6-30"};
- //可以是城市的类型判断或者第几天判断,根据不同的需求构造不同的数组结构适应不同的应用
- int []type={0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3};
- int size=data.length;
- for(int i=0;i<size;i++){
- HashMap<String, String> map = new HashMap<String, String>();
- //根据不同需求可以构造更复杂的数据,目前之构造一个数据
- map.put("data", data[i]);
- listItem.add(map);
- }
- WeatherAdapter listItemAdapter= new WeatherAdapter(this, listItem,type);
- list.setAdapter(listItemAdapter);
- }
- }
三个xml布局文件:
3.city_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!-- items选项 -->
- <RelativeLayout
- android:id="@+id/RelativeLayout"
- android:layout_width="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip"
- android:paddingLeft="12dip"
- android:paddingRight="12dip"
- android:background="#9ACD32"
- >
- <ImageView
- android:paddingTop="4dip"
- android:layout_alignParentRight="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/weather_image"
- />
- <TextView
- android:layout_height="wrap_content"
- android:textSize="18dip"
- android:layout_width="fill_parent"
- android:id="@+id/city"
- android:paddingTop="6dip"
- android:textColor="@color/black"
- />
- </RelativeLayout>
4.content_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!-- items选项 -->
- <RelativeLayout
- android:id="@+id/RelativeLayout02"
- android:layout_width="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip"
- android:paddingLeft="12dip"
- android:paddingRight="12dip"
- android:background="@drawable/listview_bg"
- >
- <ImageView
- android:paddingTop="22dip"
- android:layout_alignParentRight="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/weather_image"
- />
- <TextView
- android:layout_height="wrap_content"
- android:textSize="18dip"
- android:layout_width="fill_parent"
- android:id="@+id/content"
- android:paddingTop="6dip"
- />
- <TextView
- android:text=""
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_below="@+id/content"
- android:id="@+id/date"
- android:paddingRight="20dip"
- />
- </RelativeLayout>
5.main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ListView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:divider="@color/white"
- android:dividerHeight="1dip"
- android:id="@+id/list_items"
- />
- </LinearLayout>
源码下载:http://download.csdn.net/detail/caoshichao520326/6392923