在做一个记账的功能时,数据源是时间,用户和各项支出混合的一个类,最终需要的ListView效果是:以天为单位显示各项数据,支持下拉刷新和上拉加载,加载时也是以天为单位,
数据源和最终效果图如下
public class Consume {
private int id;
private String accountNum;// 电话
private String consumeDate;// 时间
private float consumeWear;// 衣
private float consumeFood;// 食
private float consumeLive;// 住
private float consumeWalk;// 行
private float consumeBase;// 衣食住行
private float consumeEntertainment;// 娱乐
private float consumeStudy;// 学习
private float consumeActivity;// 应酬
private float consumeOther;// 其他
}
布局有两个部分,即日期和每个具体的支出,如下
consume_group
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp" >
<TextView
android:id="@+id/consume_date"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#9BCD9B"
android:gravity="center"
android:textColor="@color/gray"
android:textSize="15sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@drawable/list_divider"
android:focusable="true"
android:focusableInTouchMode="true" />
</LinearLayout>
consume_child
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EDEDED"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EECBAD"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/consume_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="center"
android:textColor="#912CEE"
android:textSize="20sp" />
<TextView
android:id="@+id/consume_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center_vertical|right"
android:textColor="#912CEE"
android:textSize="17sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="元"
android:textSize="17sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@drawable/list_divider"
android:focusable="true"
android:focusableInTouchMode="true" />
</LinearLayout>
先对数据源进行处理,方便在BaseAdapter中使用,包括3个类
MyConsumeChild ,是consume_child中数据源,
public class MyConsumeChild {
private String name;//标题
private String info;//数据
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
MyConsume,是一天中所用的数据
package com.licry.lifepasskey.model;
import java.util.ArrayList;
import java.util.List;
public class MyConsume {
private String table;//当天日期
private List<MyConsumeChild> childList;//各项支出,List为了后续的变更,目前为8个
private Consume mConsume;
public MyConsume(Consume consume) {
mConsume = consume;
}
//将consume解析成table和childList
public String getTable() {
return table;
}
public void setTable() {
table = mConsume.getConsumeDate();
}
public List<MyConsumeChild> getList() {
return childList;
}
// "衣", "食", "住", "行", "学习", "娱乐", "应酬", "其他"
public void setList() {
childList = new ArrayList<MyConsumeChild>();
MyConsumeChild mMyConsumeChild0 = new MyConsumeChild();
mMyConsumeChild0.setName("衣");
mMyConsumeChild0.setInfo(String.valueOf(mConsume.getConsumeWear()));
childList.add(mMyConsumeChild0);
MyConsumeChild mMyConsumeChild1 = new MyConsumeChild();
mMyConsumeChild1.setName("食");
mMyConsumeChild1.setInfo(String.valueOf(mConsume.getConsumeFood()));
childList.add(mMyConsumeChild1);
MyConsumeChild mMyConsumeChild2 = new MyConsumeChild();
mMyConsumeChild2.setName("住");
mMyConsumeChild2.setInfo(String.valueOf(mConsume.getConsumeLive()));
childList.add(mMyConsumeChild2);
MyConsumeChild mMyConsumeChild3 = new MyConsumeChild();
mMyConsumeChild3.setName("行");
mMyConsumeChild3.setInfo(String.valueOf(mConsume.getConsumeWalk()));
childList.add(mMyConsumeChild3);
MyConsumeChild mMyConsumeChild4 = new MyConsumeChild();
mMyConsumeChild4.setName("学习");
mMyConsumeChild4.setInfo(String.valueOf(mConsume.getConsumeStudy()));
childList.add(mMyConsumeChild4);
MyConsumeChild mMyConsumeChild5 = new MyConsumeChild();
mMyConsumeChild5.setName("娱乐");
mMyConsumeChild5.setInfo(String.valueOf(mConsume
.getConsumeEntertainment()));
childList.add(mMyConsumeChild5);
MyConsumeChild mMyConsumeChild6 = new MyConsumeChild();
mMyConsumeChild6.setName("应酬");
mMyConsumeChild6.setInfo(String.valueOf(mConsume.getConsumeActivity()));
childList.add(mMyConsumeChild6);
MyConsumeChild mMyConsumeChild7 = new MyConsumeChild();
mMyConsumeChild7.setName("其他");
mMyConsumeChild7.setInfo(String.valueOf(mConsume.getConsumeOther()));
childList.add(mMyConsumeChild7);
}
}
主要的数据和布局适配在BaseAdapter中实现,具体代码如下
package com.licry.lifepasskey.adapter;
import java.util.ArrayList;
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.TextView;
import com.licry.lifepasskey.R;
import com.licry.lifepasskey.model.Consume;
import com.licry.lifepasskey.model.MyConsume;
import com.licry.lifepasskey.model.MyConsumeChild;
public class ConsumeAdapter extends BaseAdapter {
private List<MyConsume> mMyConsumeList;
private Context mContext;
//由数据库查得的数据是当前帐号下的所有信息,即List<Consume>,将其解析成List<MyConsume>
public ConsumeAdapter(List<Consume> consumeList, Context mContext) {
this.mContext = mContext;
mMyConsumeList = new ArrayList<MyConsume>();
for (int i = 0; i < consumeList.size(); i++) {
MyConsume mMyConsume = new MyConsume(consumeList.get(i));
mMyConsume.setTable();
mMyConsume.setList();
mMyConsumeList.add(mMyConsume);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mMyConsumeList.size() * 9;//每组有9项,再乘以list的个数,即所有个数
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
int i = (int) position / 9;
return mMyConsumeList.get(i);//得到每一组的的MyConsume
}
@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
MyConsume myConsume = (MyConsume) getItem(position);
GroupViewHolder groupViewHolder = null;
ChildViewHolder childViewHolder = null;
int k = position % 9;//确定组数,分成日期和具体支出
if (k == 0) {//填充日期数据
if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.consume_group,
null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.data = (TextView) convertView
.findViewById(R.id.consume_date);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.data.setText(myConsume.getTable());
} else {//填充支出数据
if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.consume_child,
null);
childViewHolder = new ChildViewHolder();
childViewHolder.name = (TextView) convertView
.findViewById(R.id.consume_name);
childViewHolder.info = (TextView) convertView
.findViewById(R.id.consume_info);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
MyConsumeChild myConsumeChild = myConsume.getList().get(k - 1);
childViewHolder.name.setText(myConsumeChild.getName());
childViewHolder.info.setText(myConsumeChild.getInfo());
}
return convertView;
}
class GroupViewHolder {
TextView data;
}
class ChildViewHolder {
TextView name;
TextView info;
}
}
最终在Activity中使用
mListView = (ListView) findViewById(R.id.list_view_consume);
mConsumeAdapter = new ConsumeAdapter(consumeList, this);
mListView.setAdapter(mConsumeAdapter);
总的来说在自定义ListView多思考数据和view的匹配关系,复杂的多拆解再匹配,虽然篇幅会多,但带来的是逻辑的清晰和高效。