Android开发实战——记账本(3)
开发日志(3)——适配器
昨天将bean类还有DatabaseHelper类写完。为了在MainActivity中调用,将数据保存到数据库中并显示出来。所以要先编写适配器CostListAdapter
CostListAdapter
package com.example.firstapplication;
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 java.util.List;
public class CostListAdapter extends BaseAdapter {
private List<CostBean> mList;
private Context mContext;
private LayoutInflater mLayoutInflater;
public CostListAdapter(Context context,List<CostBean> list){
mContext = context;
mList = list;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
viewHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.list_item,null);
viewHolder.mTvCostTitle = (TextView) convertView.findViewById(R.id.tv_title);
viewHolder.mTvCostDate =(TextView) convertView.findViewById(R.id.tv_date);
viewHolder.mTvCostMoney = (TextView) convertView.findViewById(R.id.tv_cost);
convertView.setTag(viewHolder);
} else{
viewHolder = (ViewHolder) convertView.getTag();
}
CostBean bean = mList.get(position);
viewHolder.mTvCostTitle.setText(bean.costTitle);
viewHolder.mTvCostDate.setText(bean.costDate);
viewHolder.mTvCostMoney.setText(bean.costMoney);
return convertView;
}
private static class ViewHolder{
public TextView mTvCostTitle;
public TextView mTvCostDate;
public TextView mTvCostMoney;
}
}
然后在MainActivity中写一个方法生成数据,并在模拟器中显示出来
明天打算写一下MainActivity中的一些方法,让他实现更多的功能