收入支出界面逻辑续写
作者:@kuaiquxie
作者的github:https://github.com/bitebita
本文为作者原创,如需转载,请注明出处:https://www.cnblogs.com/dzwj/p/15760469.html
package com.example.easycash.db; /* * 表示收入或者支出具体类型的类 * */ public class TypeBean { int id; String typename; //类型名称 int imageId; //未被选中图片id int simageId; //被选中图片id 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 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() { } 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.easycash.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; import com.example.easycash.R; public class DBOpenHelper extends SQLiteOpenHelper { public DBOpenHelper(@Nullable Context context) { super(context,"easycash.db" , null, 1); } // 创建数据库的方法,只有项目第一次运行时,会被调用 @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) { // 向typetb表当中插入元素 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 db, int oldVersion, int newVersion) { } }
package com.example.easycash.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.easycash.utils.FloatUtils; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; /* * 负责管理数据库的类 * 主要对于表当中的内容进行操作,增删改查 * */ public class DBManager { private static SQLiteDatabase db; /* 初始化数据库对象*/ public static void initDB(Context context){ DBOpenHelper helper = new DBOpenHelper(context); //得到帮助类对象 db = helper.getWritableDatabase(); //得到数据库对象 } /** * 读取数据库当中的数据,写入内存集合里 * kind :表示收入或者支出 * */ @SuppressLint("Range") public static List<TypeBean>getTypeList(int kind){ List<TypeBean>list = new ArrayList<>(); //读取typetb表当中的数据 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); } /* * 获取记账表当中某一天的所有支出或者收入情况 * */ @SuppressLint("Range") 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; } /* * 获取记账表当中某一月的所有支出或者收入情况 * */ @SuppressLint("Range") 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; } /** * 获取某一天的支出或者收入的总金额 kind:支出==0 收入===1 * */ @SuppressLint("Range") 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 * */ @SuppressLint("Range") 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; } /** 统计某月份支出或者收入情况有多少条 收入-1 支出-0*/ @SuppressLint("Range") 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 * */ @SuppressLint("Range") 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; } /** * 根据备注搜索收入或者支出的情况列表 * */ @SuppressLint("Range") 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; } /** * 查询记账的表当中有几个年份信息 * */ @SuppressLint("Range") 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); } /** * 查询指定年份和月份的收入或者支出每一种类型的总钱数 * */ @SuppressLint("Range") 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; } /** * 获取这个月当中某一天收入支出最大的金额,金额是多少 * */ @SuppressLint("Range") 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; } /** 根据指定月份每一日收入或者支出的总钱数的集合*/ @SuppressLint("Range") 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; } }
package com.example.easycash.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.easycash.R; import com.example.easycash.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; } }
package com.example.easycash.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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)