家庭记账本(四)保存信息

  首先我们在数据库中创建一张表,用来存储收支信息。

public class DBOpenHelper extends SQLiteOpenHelper {
    public DBOpenHelper(@Nullable Context context) {
        super(context, "tally.db", null, 1);
    }

    /*创建数据库的方法
    * 只有项目第一次运行时会被调用*/
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table tb_type(id integer primary key autoincrement, " +
                "typename varchar(10)," +
                "imageId integer," +
                "sImageId integer," +
                "kind integer)";
        db.execSQL(sql);

        insertType(db);

        /*创建记账表*/
        String sql2 = "create table tb_account(id integer primary key autoincrement, " +
                "typename varchar(10), " +
                "sImageId integer, " +
                "mark varchar(80), " +
                "money float, " +
                "time varchar(60), " +
                "year integer, " +
                "month integer, " +
                "day integer, " +
                "kind integer) ";
        db.execSQL(sql2);
    }

    /**
     * 数据库版本在更新时发生改变,会调用此方法*/
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insertType(SQLiteDatabase db) {
        //向tb_type表中插入元素
        String sql = "insert into tb_type(typename, imageId, sImageId, kind) values(?,?,?,?)";
        db.execSQL(sql, new Object[]{"其它", R.mipmap.ic_other, R.mipmap.ic_other, 0});
        db.execSQL(sql, new Object[]{"餐饮", R.mipmap.ic_catering, R.mipmap.ic_catering_selected, 0});
        db.execSQL(sql, new Object[]{"日用", R.mipmap.ic_daily, R.mipmap.ic_daily_selected, 0});
        db.execSQL(sql, new Object[]{"水果", R.mipmap.ic_fruit, R.mipmap.ic_fruit_selected, 0});
        db.execSQL(sql, new Object[]{"娱乐", R.mipmap.ic_recreation, R.mipmap.ic_recreation_selected, 0});
        db.execSQL(sql, new Object[]{"宠物", R.mipmap.ic_pet, R.mipmap.ic_pet_selected, 0});
        db.execSQL(sql, new Object[]{"学习", R.mipmap.ic_study, R.mipmap.ic_study_selected, 0});
        db.execSQL(sql, new Object[]{"交通", R.mipmap.ic_traffic, R.mipmap.ic_traffic_selected, 0});
        db.execSQL(sql, new Object[]{"医疗", R.mipmap.ic_medical, R.mipmap.ic_medical_selected, 0});

        db.execSQL(sql, new Object[]{"其它", R.mipmap.ic_other, R.mipmap.ic_other, 1});
        db.execSQL(sql, new Object[]{"工资", R.mipmap.ic_salary, R.mipmap.ic_salary_selected, 1});
        db.execSQL(sql, new Object[]{"兼职", R.mipmap.ic_part_time, R.mipmap.ic_part_time_selected, 1});
        db.execSQL(sql, new Object[]{"理财", R.mipmap.ic_transaction, R.mipmap.ic_transaction_selected, 1});
        db.execSQL(sql, new Object[]{"礼金", R.mipmap.ic_gift, R.mipmap.ic_gift_selected, 1});

    }
}

  这个类我们之前是创建过的,在里面有添加了一张记账表。如果你之前运行过程序,这里需要将以前的app卸载掉重新下载。因为这里面的类只有在第一次项目运行时才会执行。

 

  对应的,我们建一个实体类,对应表中的数据。

/**
 * 描述记录一条数据的相关内容类
 */
public class AccountBean {
    private int id;
    private String typename;
    private int sImageId;
    private String mark;
    private float money;
    private String time;
    private int year;
    private int month;
    private int day;
    private int kind;

    public AccountBean() {
    }

    public AccountBean(int id, String typename, int sImageId, String mark, float money, String time, int year, int month, int day, int kind) {
        this.id = id;
        this.typename = typename;
        this.sImageId = sImageId;
        this.mark = mark;
        this.money = money;
        this.time = time;
        this.year = year;
        this.month = month;
        this.day = day;
        this.kind = kind;
    }

    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 getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

    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 static void addAccount(AccountBean bean) {
        ContentValues values = new ContentValues();
        values.put("typename", bean.getTypename());
        values.put("sImageId", bean.getsImageId());
        values.put("mark", bean.getMark());
        values.put("money", bean.getMoney());
        values.put("time", bean.getTime());
        values.put("day", bean.getDay());
        values.put("month", bean.getMonth());
        values.put("year", bean.getYear());
        values.put("kind", bean.getKind());

        db.insert("tb_account", null, values);
    }

 

  我们希望点击了自定义软键盘的确定按钮来进行保存并返回至主界面,所以在确定按钮添加监听事件。而在之前我们已经将这个点击事件增加了接口,我们可以直接编写这个接口的onEnsure方法进行功能的实现。

keyboard.setOnEnsureListener(new KeyboardUtils.OnEnsureListener() {
            @Override
            public void onEnsure() {
                /*点击了确定按钮
                * 获取记录的信息保存在数据库当中
                * 返回上一级页面
                * */
                //获取输入钱数
                String moneyStr = et_money.getText().toString();
                if (!TextUtils.isEmpty(moneyStr) || "0".equals(moneyStr)) {
                    getActivity().finish();
                }
                float money = Float.parseFloat(moneyStr);
                accountBean.setMoney(money);

                //获取记录信息,保存至数据库当中
                saveAccount();
                //返回上一级页面
                getActivity().finish();
            }
        });

 

  另外,当我们想要保存数据的时候需要传入一个AccountBean对象,那么我们需要在每一个更改了实体类数据的地方进行数据的set

posted @ 2021-01-26 22:09  Gazikel  阅读(107)  评论(0编辑  收藏  举报