安卓家庭记账本开发笔记8(补2月4日,2月5日,2月7日,2月8日)
完成收支记录界面的按钮的监听以及点击事件的逻辑编写
在后端模块创建一个名为frag_record的软件包,在其中创建三个java类,其中两个分别对应支出和收入,因为两者基本逻辑相同,所以将两者相同的部分提取出来写一个通用java类。然后支出和收入的java类继承于通用类,然后相应的不同功能在其中重写方法即可。
通用类代码如下:
package com.example.myapplication1.frag_record;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.myapplication1.R;
import com.example.myapplication1.db.AccountBean;
import com.example.myapplication1.db.DBManger;
import com.example.myapplication1.db.TypeBean;
import com.example.myapplication1.utils.BeiZhuDialog;
import com.example.myapplication1.utils.KeyBoardUtils;
import com.example.myapplication1.utils.SelectTimeDialog;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* 记录页面当中的支出模块
*/
public abstract class BaseRecordFragment extends Fragment implements View.OnClickListener {
KeyboardView keyboardView;
EditText moneyEt;
ImageView typeIv;
TextView typeTv,beizhuTv,timeTv;
GridView typeGv;
List<TypeBean>typeList;
TypeBaseAdapter adapter;
AccountBean accountBean; //将需要插入到记账本当中的数据保存成对象的形式
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
accountBean = new AccountBean(); //创建对象
accountBean.setTypename("其他");
accountBean.setsImageId(R.mipmap.ic_qita_fs);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_outcome, container, false);
initView(view);
setInitTime();
//给GridView填充数据的方法
loadDataToGV();
setGVListener(); //设置GridView每一项的点击事件
return view;
}
/* 获取当前时间,显示在timeTv上*/
private void setInitTime() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
String time = sdf.format(date);
timeTv.setText(time);
accountBean.setTime(time);
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
accountBean.setYear(year);
accountBean.setMonth(month);
accountBean.setDay(day);
}
/* 设置GridView每一项的点击事件*/
private void setGVListener() {
typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.selectPos = position;
adapter.notifyDataSetInvalidated(); //提示绘制发生变化了
TypeBean typeBean = typeList.get(position);
String typename = typeBean.getTypename();
typeTv.setText(typename);
accountBean.setTypename(typename);
int simageId = typeBean.getSimageId();
typeIv.setImageResource(simageId);
accountBean.setsImageId(simageId);
}
});
}
/* 给GridView填出数据的方法*/
public void loadDataToGV() {
typeList = new ArrayList<>();
adapter = new TypeBaseAdapter(getContext(), typeList);
typeGv.setAdapter(adapter);
}
private void initView(View view) {
keyboardView = view.findViewById(R.id.frag_record_keyboard);
moneyEt = view.findViewById(R.id.frag_record_et_money);
typeIv = view.findViewById(R.id.frag_record_iv);
typeGv = view.findViewById(R.id.frag_record_gv);
typeTv = view.findViewById(R.id.frag_record_tv_type);
beizhuTv = view.findViewById(R.id.frag_record_tv_beizhu);
timeTv = view.findViewById(R.id.frag_record_tv_time);
beizhuTv.setOnClickListener(this);
timeTv.setOnClickListener(this);
//让自定义软键盘显示出来
KeyBoardUtils boardUtils = new KeyBoardUtils(keyboardView, moneyEt);
boardUtils.showKeyboard();
//设置接口,监听确定按钮按钮被点击了
boardUtils.setOnEnsureListener(new KeyBoardUtils.OnEnsureListener() {
@Override
public void onEnsure() {
//获取输入钱数
String moneyStr = moneyEt.getText().toString();
if (TextUtils.isEmpty(moneyStr)||moneyStr.equals("0")) {
getActivity().finish();
return;
}
float money = Float.parseFloat(moneyStr);
accountBean.setMoney(money);
//获取记录的信息,保存在数据库当中
saveAccountToDB();
// 返回上一级页面
getActivity().finish();
}
});
}
/* 让子类一定要重写这个方法*/
public abstract void saveAccountToDB();
}
收入类代码:
package com.example.myapplication1.frag_record;
import com.example.myapplication1.R;
import com.example.myapplication1.db.DBManger;
import com.example.myapplication1.db.TypeBean;
import java.util.List;
public class IncomeFragment extends BaseRecordFragment {
@Override
public void loadDataToGV() {
super.loadDataToGV();
//获取数据库当中的数据源
List<TypeBean> inlist = DBManger.getTypeList(1);
typeList.addAll(inlist);
adapter.notifyDataSetChanged();
typeTv.setText("其他");
typeIv.setImageResource(R.mipmap.in_qt_fs);
}
@Override
public void saveAccountToDB() {
accountBean.setKind(1);
DBManger.insertItemToAccounttb(accountBean);
}
}
支出类代码:
package com.example.myapplication1.frag_record;
import com.example.myapplication1.R;
import com.example.myapplication1.db.DBManger;
import com.example.myapplication1.db.TypeBean;
import java.util.List;
public class OutcomeFragment extends BaseRecordFragment {
// 重写
@Override
public void loadDataToGV() {
super.loadDataToGV();
//获取数据库当中的数据源
List<TypeBean> outlist = DBManger.getTypeList(0);
typeList.addAll(outlist);
adapter.notifyDataSetChanged();
typeTv.setText("其他");
typeIv.setImageResource(R.mipmap.ic_qita_fs);
}
@Override
public void saveAccountToDB() {
accountBean.setKind(0);
DBManger.insertItemToAccounttb(accountBean);
}
}
上述代码涉及的item_recordfrag_gv界面代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="@+id/item_recordfrag_iv"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:src="@mipmap/ic_qita_fs"/>
<TextView
android:id="@+id/item_recordfrag_tv"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="其他"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:textColor="@color/grey1"
android:gravity="center"/>
</LinearLayout>
上述代码涉及的相关数据库操作需要在后端模块创建一个软件包,在其中创建四个java类,两个是相关数据类型,分别是图片和记录的账本文字信息,一个是在数据库中创建一个表用于存储数据,一个是对表中内容进行增删改查操作方法的代码
图片数据类型代码如下:
package com.example.myapplication1.db;
public class TypeBean {
int id;
String typename;
int imageId;//未被选中的图片id
int simageId;//被选中的图片id
int kind;//收入-1,支出0
public TypeBean() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public int getSimageId() {
return simageId;
}
public void setSimageId(int simageId) {
this.simageId = simageId;
}
public int getKind() {
return kind;
}
public void setKind(int kind) {
this.kind = kind;
}
public TypeBean(int id, String typename, int imageId, int simageId, int kind) {
this.id = id;
this.typename = typename;
this.imageId = imageId;
this.simageId = simageId;
this.kind = kind;
}
}
记账文字数据类型代码:
package com.example.myapplication1.db;
public class AccountBean {
int id;
String typename; //类型
int sImageId; //被选中类型图片
String beizhu; //备注
float money; //价格
String time ; //保存时间字符串
int year;
int month;
int day;
int kind; //类型 收入---1 支出---0
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
public int getsImageId() {
return sImageId;
}
public void setsImageId(int sImageId) {
this.sImageId = sImageId;
}
public String getBeizhu() {
return beizhu;
}
public void setBeizhu(String beizhu) {
this.beizhu = beizhu;
}
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getKind() {
return kind;
}
public void setKind(int kind) {
this.kind = kind;
}
public AccountBean() {
}
public AccountBean(int id, String typename, int sImageId, String beizhu, float money, String time, int year, int month, int day, int kind) {
this.id = id;
this.typename = typename;
this.sImageId = sImageId;
this.beizhu = beizhu;
this.money = money;
this.time = time;
this.year = year;
this.month = month;
this.day = day;
this.kind = kind;
}
}
创建数据库代码:
package com.example.myapplication1.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.example.myapplication1.R;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(@Nullable Context context) {
super(context, "tally.db", null, 1);
}
//创建数据库的方法,只有项目第一次运行时会被调用
int id;
String typename;
int imageId;//未被选中的图片id
int simageId;//被选中的图片id
int kind;//收入-1,支出0
@Override
public void onCreate(SQLiteDatabase db) {
//创建表示类型的表
String sql="create table typetb(id integer primary key autoincrement,typename varchar(10),imageId integer,simageId integer,kind integer)";
db.execSQL(sql);
insertType(db);
sql = "create table accounttb(id integer primary key autoincrement,typename varchar(10),sImageId integer,beizhu varchar(80),money float," +
"time varchar(60),year integer,month integer,day integer,kind integer)";
db.execSQL(sql);
}
private void insertType(SQLiteDatabase db){
String sql="insert into typetb(typename,imageId,simageId,kind) values(?,?,?,?)";
db.execSQL(sql,new Object[]{"其他", R.mipmap.ic_qita, R.mipmap.ic_qita_fs,0});
db.execSQL(sql,new Object[]{"餐饮", R.mipmap.ic_canyin, R.mipmap.ic_canyin_fs,0});
db.execSQL(sql,new Object[]{"交通", R.mipmap.ic_jiaotong, R.mipmap.ic_jiaotong_fs,0});
db.execSQL(sql,new Object[]{"购物", R.mipmap.ic_gouwu, R.mipmap.ic_gouwu_fs,0});
db.execSQL(sql,new Object[]{"服饰", R.mipmap.ic_fushi, R.mipmap.ic_fushi_fs,0});
db.execSQL(sql,new Object[]{"日用品", R.mipmap.ic_riyongpin, R.mipmap.ic_riyongpin_fs,0});
db.execSQL(sql,new Object[]{"娱乐", R.mipmap.ic_yule, R.mipmap.ic_yule_fs,0});
db.execSQL(sql,new Object[]{"零食", R.mipmap.ic_lingshi, R.mipmap.ic_lingshi_fs,0});
db.execSQL(sql,new Object[]{"烟酒茶", R.mipmap.ic_yanjiu, R.mipmap.ic_yanjiu_fs,0});
db.execSQL(sql,new Object[]{"学习", R.mipmap.ic_xuexi, R.mipmap.ic_xuexi_fs,0});
db.execSQL(sql,new Object[]{"医疗", R.mipmap.ic_yiliao, R.mipmap.ic_yiliao_fs,0});
db.execSQL(sql,new Object[]{"住宅", R.mipmap.ic_zhufang, R.mipmap.ic_zhufang_fs,0});
db.execSQL(sql,new Object[]{"水电煤", R.mipmap.ic_shuidianfei, R.mipmap.ic_shuidianfei_fs,0});
db.execSQL(sql,new Object[]{"通讯", R.mipmap.ic_tongxun, R.mipmap.ic_tongxun_fs,0});
db.execSQL(sql,new Object[]{"人情往来", R.mipmap.ic_renqingwanglai, R.mipmap.ic_renqingwanglai_fs,0});
db.execSQL(sql,new Object[]{"其他", R.mipmap.in_qt, R.mipmap.in_qt_fs,1});
db.execSQL(sql,new Object[]{"薪资", R.mipmap.in_xinzi, R.mipmap.in_xinzi_fs,1});
db.execSQL(sql,new Object[]{"奖金", R.mipmap.in_jiangjin, R.mipmap.in_jiangjin_fs,1});
db.execSQL(sql,new Object[]{"借入", R.mipmap.in_jieru, R.mipmap.in_jieru_fs,1});
db.execSQL(sql,new Object[]{"收债", R.mipmap.in_shouzhai, R.mipmap.in_shouzhai_fs,1});
db.execSQL(sql,new Object[]{"利息收入", R.mipmap.in_lixifuji, R.mipmap.in_lixifuji_fs,1});
db.execSQL(sql,new Object[]{"投资回报", R.mipmap.in_touzi, R.mipmap.in_touzi_fs,1});
db.execSQL(sql,new Object[]{"二手交易", R.mipmap.in_ershoushebei, R.mipmap.in_ershoushebei_fs,1});
db.execSQL(sql,new Object[]{"意外所得", R.mipmap.in_yiwai, R.mipmap.in_yiwai_fs,1});
}
//数据库版本在更新时发生改变,会调用此方法
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
数据库增删改查代码:
package com.example.myapplication1.db;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.example.myapplication1.utils.FloatUtils;
import java.util.ArrayList;
import java.util.List;
public class DBManger {
private static SQLiteDatabase db;
public static void initDB(Context context)
{
DBOpenHelper helper = new DBOpenHelper(context);
db=helper.getWritableDatabase();
}
public static List<TypeBean> getTypeList(int kind)
{
List<TypeBean> list = new ArrayList<>();
String sql="select * from typetb where kind = "+ kind;
Cursor cursor = db.rawQuery(sql, null);
while(cursor.moveToNext())
{
String typename=cursor.getString(cursor.getColumnIndex("typename"));
int imageId=cursor.getInt(cursor.getColumnIndex("imageId"));
int simageId=cursor.getInt(cursor.getColumnIndex("simageId"));
int kind1=cursor.getInt(cursor.getColumnIndex("kind"));
int id=cursor.getInt(cursor.getColumnIndex("id"));
TypeBean typeBean=new TypeBean(id,typename,imageId,simageId,kind);
list.add(typeBean);
}
return list;
}
public static void insertItemToAccounttb(AccountBean bean){
ContentValues values = new ContentValues();
values.put("typename",bean.getTypename());
values.put("sImageId",bean.getsImageId());
values.put("beizhu",bean.getBeizhu());
values.put("money",bean.getMoney());
values.put("time",bean.getTime());
values.put("year",bean.getYear());
values.put("month",bean.getMonth());
values.put("day",bean.getDay());
values.put("kind",bean.getKind());
db.insert("accounttb",null,values);
}
public static List<AccountBean>getAccountListOneDayFromAccounttb(int year,int month,int day){
List<AccountBean>list = new ArrayList<>();
String sql = "select * from accounttb where year=? and month=? and day=? order by id desc";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", day + ""});
//遍历符合要求的每一行数据
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String typename = cursor.getString(cursor.getColumnIndex("typename"));
String beizhu = cursor.getString(cursor.getColumnIndex("beizhu"));
String time = cursor.getString(cursor.getColumnIndex("time"));
int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId"));
int kind = cursor.getInt(cursor.getColumnIndex("kind"));
float money = cursor.getFloat(cursor.getColumnIndex("money"));
AccountBean accountBean = new AccountBean(id, typename, sImageId, beizhu, money, time, year, month, day, kind);
list.add(accountBean);
}
return list;
}
public static List<AccountBean>getAccountListOneMonthFromAccounttb(int year,int month){
List<AccountBean>list = new ArrayList<>();
String sql = "select * from accounttb where year=? and month=? order by id desc";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + ""});
//遍历符合要求的每一行数据
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String typename = cursor.getString(cursor.getColumnIndex("typename"));
String beizhu = cursor.getString(cursor.getColumnIndex("beizhu"));
String time = cursor.getString(cursor.getColumnIndex("time"));
int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId"));
int kind = cursor.getInt(cursor.getColumnIndex("kind"));
float money = cursor.getFloat(cursor.getColumnIndex("money"));
int day = cursor.getInt(cursor.getColumnIndex("day"));
AccountBean accountBean = new AccountBean(id, typename, sImageId, beizhu, money, time, year, month, day, kind);
list.add(accountBean);
}
return list;
}
public static float getSumMoneyOneDay(int year,int month,int day,int kind){
float total = 0.0f;
String sql = "select sum(money) from accounttb where year=? and month=? and day=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", day + "", kind + ""});
// 遍历
if (cursor.moveToFirst()) {
float money = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
total = money;
}
return total;
}
/**
* 获取某一月的支出或者收入的总金额 kind:支出==0 收入===1
* */
public static float getSumMoneyOneMonth(int year,int month,int kind){
float total = 0.0f;
String sql = "select sum(money) from accounttb where year=? and month=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
// 遍历
if (cursor.moveToFirst()) {
float money = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
total = money;
}
return total;
}
public static int getCountItemOneMonth(int year,int month,int kind){
int total = 0;
String sql = "select count(money) from accounttb where year=? and month=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
if (cursor.moveToFirst()) {
int count = cursor.getInt(cursor.getColumnIndex("count(money)"));
total = count;
}
return total;
}
/**
* 获取某一年的支出或者收入的总金额 kind:支出==0 收入===1
* */
public static float getSumMoneyOneYear(int year,int kind){
float total = 0.0f;
String sql = "select sum(money) from accounttb where year=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", kind + ""});
// 遍历
if (cursor.moveToFirst()) {
float money = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
total = money;
}
return total;
}
/*
* 根据传入的id,删除accounttb表当中的一条数据
* */
public static int deleteItemFromAccounttbById(int id){
int i = db.delete("accounttb", "id=?", new String[]{id + ""});
return i;
}
/**
* 根据备注搜索收入或者支出的情况列表
* */
public static List<AccountBean>getAccountListByRemarkFromAccounttb(String beizhu){
List<AccountBean>list = new ArrayList<>();
String sql = "select * from accounttb where beizhu like '%"+beizhu+"%'";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String typename = cursor.getString(cursor.getColumnIndex("typename"));
String bz = cursor.getString(cursor.getColumnIndex("beizhu"));
String time = cursor.getString(cursor.getColumnIndex("time"));
int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId"));
int kind = cursor.getInt(cursor.getColumnIndex("kind"));
float money = cursor.getFloat(cursor.getColumnIndex("money"));
int year = cursor.getInt(cursor.getColumnIndex("year"));
int month = cursor.getInt(cursor.getColumnIndex("month"));
int day = cursor.getInt(cursor.getColumnIndex("day"));
AccountBean accountBean = new AccountBean(id, typename, sImageId, bz, money, time, year, month, day, kind);
list.add(accountBean);
}
return list;
}
/**
* 查询记账的表当中有几个年份信息
* */
public static List<Integer>getYearListFromAccounttb(){
List<Integer>list = new ArrayList<>();
String sql = "select distinct(year) from accounttb order by year asc";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
int year = cursor.getInt(cursor.getColumnIndex("year"));
list.add(year);
}
return list;
}
/*
* 删除accounttb表格当中的所有数据
* */
public static void deleteAllAccount(){
String sql = "delete from accounttb";
db.execSQL(sql);
}
/**
* 查询指定年份和月份的收入或者支出每一种类型的总钱数
* */
public static List<ChartItemBean>getChartListFromAccounttb(int year,int month,int kind){
List<ChartItemBean>list = new ArrayList<>();
float sumMoneyOneMonth = getSumMoneyOneMonth(year, month, kind); //求出支出或者收入总钱数
String sql = "select typename,sImageId,sum(money)as total from accounttb where year=? and month=? and kind=? group by typename " +
"order by total desc";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
while (cursor.moveToNext()) {
int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId"));
String typename = cursor.getString(cursor.getColumnIndex("typename"));
float total = cursor.getFloat(cursor.getColumnIndex("total"));
//计算所占百分比 total /sumMonth
float ratio = FloatUtils.div(total,sumMoneyOneMonth);
ChartItemBean bean = new ChartItemBean(sImageId, typename, ratio, total);
list.add(bean);
}
return list;
}
/**
* 获取这个月当中某一天收入支出最大的金额,金额是多少
* */
public static float getMaxMoneyOneDayInMonth(int year,int month,int kind){
String sql = "select sum(money) from accounttb where year=? and month=? and kind=? group by day order by sum(money) desc";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
if (cursor.moveToFirst()) {
float money = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
return money;
}
return 0;
}
/** 根据指定月份每一日收入或者支出的总钱数的集合*/
public static List<BarChartItemBean>getSumMoneyOneDayInMonth(int year,int month,int kind){
String sql = "select day,sum(money) from accounttb where year=? and month=? and kind=? group by day";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
List<BarChartItemBean>list = new ArrayList<>();
while (cursor.moveToNext()) {
int day = cursor.getInt(cursor.getColumnIndex("day"));
float smoney = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
BarChartItemBean itemBean = new BarChartItemBean(year, month, day, smoney);
list.add(itemBean);
}
return list;
}
}
上述代码中收入支出界面关于type的适配器代码如下:
package com.example.myapplication1.frag_record;
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;
import com.example.myapplication1.R;
import com.example.myapplication1.db.TypeBean;
import java.util.List;
public class TypeBaseAdapter extends BaseAdapter {
Context context;
List<TypeBean> mDatas;
int selectPos = 0; //选中位置
public TypeBaseAdapter(Context context, List<TypeBean> mDatas) {
this.context = context;
this.mDatas = mDatas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
// 此适配器不考虑复用问题,因为所有的item都显示在界面上,不会因为滑动就消失,所有没有剩余的convertView,所以不用复写
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_recordfrag_gv,parent,false);
//查找布局当中的控件
ImageView iv = convertView.findViewById(R.id.item_recordfrag_iv);
TextView tv = convertView.findViewById(R.id.item_recordfrag_tv);
//获取指定位置的数据源
TypeBean typeBean = mDatas.get(position);
tv.setText(typeBean.getTypename());
// 判断当前位置是否为选中位置,如果是选中位置,就设置为带颜色的图片,否则为灰色图片
if (selectPos == position) {
iv.setImageResource(typeBean.getSimageId());
}else{
iv.setImageResource(typeBean.getImageId());
}
return convertView;
}
}