记账本
记录支出界面以及数据库插入信息
BaseRecordFragment.java
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(); @Override public void onClick(View v) { switch (v.getId()) { case R.id.frag_record_tv_time: showTimeDialog(); break; case R.id.frag_record_tv_beizhu: showBZDialog(); break; } } /* 弹出显示时间的对话框*/ private void showTimeDialog() { SelectTimeDialog dialog = new SelectTimeDialog(getContext()); dialog.show(); //设定确定按钮被点击了的监听器 dialog.setOnEnsureListener(new SelectTimeDialog.OnEnsureListener() { @Override public void onEnsure(String time, int year, int month, int day) { timeTv.setText(time); accountBean.setTime(time); accountBean.setYear(year); accountBean.setMonth(month); accountBean.setDay(day); } }); } /* 弹出备注对话框*/ public void showBZDialog(){ final BeiZhuDialog dialog = new BeiZhuDialog(getContext()); dialog.show(); dialog.setDialogSize(); dialog.setOnEnsureListener(new BeiZhuDialog.OnEnsureListener() { @Override public void onEnsure() { String msg = dialog.getEditText(); if (!TextUtils.isEmpty(msg)) { beizhuTv.setText(msg); accountBean.setBeizhu(msg); } dialog.cancel(); } }); } }
DBManager.java
public class DBManager { private static SQLiteDatabase db; /* 初始化数据库对象*/ public static void initDB(Context context){ DBOpenHelper helper = new DBOpenHelper(context); //得到帮助类对象 db = helper.getWritableDatabase(); //得到数据库对象 } /** * 读取数据库当中的数据,写入内存集合里 * kind :表示收入或者支出 * */ 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); } /* * 获取记账表当中某一天的所有支出或者收入情况 * */ 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; } /** * 获取某一天的支出或者收入的总金额 kind:支出==0 收入===1 * */ 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; } /** 统计某月份支出或者收入情况有多少条 收入-1 支出-0*/ 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; } }
AccountBean.java
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; } }
BeiZhuDialog.java
public class BeiZhuDialog extends Dialog implements View.OnClickListener { EditText et; Button cancelBtn,ensureBtn; OnEnsureListener onEnsureListener; // 设定回调接口的方法 public void setOnEnsureListener(OnEnsureListener onEnsureListener) { this.onEnsureListener = onEnsureListener; } public BeiZhuDialog(@NonNull Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_beizhu);//设置对话框显示布局 et = findViewById(R.id.dialog_beizhu_et); cancelBtn = findViewById(R.id.dialog_beizhu_btn_cancel); ensureBtn = findViewById(R.id.dialog_beizhu_btn_ensure); cancelBtn.setOnClickListener(this); ensureBtn.setOnClickListener(this); } public interface OnEnsureListener{ public void onEnsure(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.dialog_beizhu_btn_cancel: cancel(); break; case R.id.dialog_beizhu_btn_ensure: if (onEnsureListener!=null) { onEnsureListener.onEnsure(); } break; } } // 获取输入数据的方法 public String getEditText(){ return et.getText().toString().trim(); } /* 设置Dialog的尺寸和屏幕尺寸一致*/ public void setDialogSize(){ // 获取当前窗口对象 Window window = getWindow(); // 获取窗口对象的参数 WindowManager.LayoutParams wlp = window.getAttributes(); // 获取屏幕宽度 Display d = window.getWindowManager().getDefaultDisplay(); wlp.width = (int)(d.getWidth()); //对话框窗口为屏幕窗口 wlp.gravity = Gravity.BOTTOM; window.setBackgroundDrawableResource(android.R.color.transparent); window.setAttributes(wlp); handler.sendEmptyMessageDelayed(1,100); } Handler handler = new Handler(){ @Override public void handleMessage(@NonNull Message msg) { //自动弹出软键盘的方法 InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS); } }; }

浙公网安备 33010602011771号