一、说在前面

昨天 学习了数据库的一些简单操作
今天 使用数据库,完成对记账本的账单记录的增删
问题 没有

二、数据库

1、账单表的结构

(注 id:账单的唯一标识,uid:记录账单的用户的id,cost_time:记录账单的时间,cost_type:账单类型,cost_money:账单金额。)

2、Entity:AccountRecord.java

package com.me.familybookkeepingbook;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey; @Entity
public class AccountRecord {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "uid")
private int uid;
@ColumnInfo(name = "cost_time")
private String costTime;
@ColumnInfo(name = "cost_type")
private String costType;
@ColumnInfo(name = "cost_money")
private Double costMoney; public AccountRecord(int uid, String costTime, String costType, Double costMoney) {
this.uid = uid;
this.costTime = costTime;
this.costType = costType;
this.costMoney = costMoney;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public int getUid() {
return uid;
} public void setUid(int uid) {
this.uid = uid;
} public String getCostTime() {
return costTime;
} public void setCostTime(String costTime) {
this.costTime = costTime;
} public String getCostType() {
return costType;
} public void setCostType(String costType) {
this.costType = costType;
} public Double getCostMoney() {
return costMoney;
} public void setCostMoney(Double costMoney) {
this.costMoney = costMoney;
}
}

3、对数据库的操作:AccountRecordDao.java

package com.me.familybookkeepingbook;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update; import java.util.List; @Dao
public interface AccountRecordDao {
@Insert
void insertAccountRecord (AccountRecord ... AccountRecords);
@Update
void updateAccountRecord (AccountRecord ... AccountRecords);
@Delete
void deleteAccountRecord (AccountRecord ... AccountRecords);
@Query("DELETE From ACCOUNTRECORD")
void deleteAllAccountRecord ();
@Query("SELECT * From ACCOUNTRECORD ORDER BY ID DESC")
List<AccountRecord> getAllAccountRecord ();
}

AccountRecordDatabase.java

package com.me.familybookkeepingbook;

import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities = {AccountRecord.class },version = 1,exportSchema = false)
public abstract class AccountRecordDatabase extends RoomDatabase {
public abstract AccountRecordDao getAccountRecordDao();
}

三、界面布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" /> <ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"> <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/textview"
android:textSize="24sp" />
</ScrollView> <Button
android:id="@+id/buttonInsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonInsert"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/buttonDelete"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" /> <Button
android:id="@+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonDalete"
app:layout_constraintBottom_toBottomOf="@+id/buttonInsert"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/buttonInsert"
app:layout_constraintTop_toTopOf="@+id/buttonInsert" />
</androidx.constraintlayout.widget.ConstraintLayout>

四、界面和数据的绑定

1、界面的更新

void updateView(){
List<AccountRecord> list = accountRecordDao.getAllAccountRecord();
String text = "";
for (int i=0;i<list.size();i++){
AccountRecord accountRecord = list.get(i);
text += accountRecord.getId() + " " + accountRecord.getCostType() + " "+accountRecord.getCostTime() +" "+
accountRecord.getCostMoney() +"\n";
}
textView.setText(text);
}

2、按键的监听

 buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AccountRecord accountRecord = new AccountRecord(1,"2020-01-26","学习",100.0);
accountRecordDao.insertAccountRecord(accountRecord);
updateView();
}
});
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
accountRecordDao.deleteAllAccountRecord();
updateView();
}
});

五、运行测试

1、插入:

2、删除

【Android】家庭记账本手机版开发报告一的更多相关文章

  1. 【Android】家庭记账本手机版开发报告五

    一.说在前面   昨天  1.添加菜单(查询.清除所有等)2.使用滑动删除   今天 1.创建登入和注册界面 2.向数据库添加一张用户表   问题 做完后在登入时有bug(未解决) 二.界面的搭建 1 ...

  2. 【Android】家庭记账本手机版开发报告二

    一.说在前面 昨天 完成了对记账本的账单的增删 今天 完善昨天的框架结构( 1.引入ViewModel管理数据.使MainActive 只管理界面.不再管数据了 2.引入AsyncTask.后台执行. ...

  3. 【Android】家庭记账本手机版开发报告七

    一.说在前面  昨天 实现了账单的图标显示  今天 本地化,测试APP,将工程源码放到github上 源码:https://github.com/xiaotian12-call/Android_Boo ...

  4. 【Android】家庭记账本手机版开发报告六

    一.说在前面  昨天 1.创建登入和注册界面:2.向数据库添加一张用户表  今天 用图标显示账单情况 问题 1.使用第三方库 hellochart,时添加依赖构建失败 2.在 chertFragmen ...

  5. 【Android】家庭记账本手机版开发报告四

    一.说在前面 昨天 对界面显示和逻辑结构进行完善 今天 1.添加菜单(查询.清除所有等) 2.使用滑动删除 问题 1.在做查询时获取SearchView时引 入包错误经过长时间的尝试后才修正 2.滑动 ...

  6. 【Android】家庭记账本手机版开发报告三

    一.说在前面 昨天 对第一天的框架结构进行了四方面的完善 今天 对界面显示和逻辑结构进行完善 问题 无 二.界面展示完善 1.使用可回收的列表recyclerView展示账单的信息,并设置数据项为卡片 ...

  7. WPS Office手机版调用接口代码指导帖之一(Android)

    经常会有一些喜欢开发鼓捣的童鞋问我们,WPS Office手机版是否提供调用接口,希望在android中使用一个调用命令,直接调用WPS手机版来打开指定的DOC文件,而不用弹出一个程序可选列表(如果用 ...

  8. 手机版WEB开发经验分享,手机版网站开发注意事项,网站自适应,手机版网站自适应,移动安卓APP自适应

    转自 http://my.oschina.net/cart/blog/282477 做前端开发不短了,用过jQuery Mobile jqMobi 也纯手工写过.. 最后总结如下: jQuery Mo ...

  9. Android实战项目——家庭记账本设计思路

    经过三周左右的Android学习,实感只有上手开发才能有所提高.在此打算做一个家庭记账APP,同时巩固一下学到的东西并且弥补漏洞. 概述 记账是自古以来人类必不可少的一件事,从古代的算盘,到手写账本, ...

随机推荐

  1. 第1节 storm编程:3、storm的架构模型的介绍

    nimbus:主节点,接收客户端提交的任务,并且分配任务,新的版本当中nimbus已经可以有多个了 zookeeper集群:storm依靠zk来保存一些节点信息,nimbus将分配的任务信息都写入到z ...

  2. VUE 使用axios请求第三方接口数据跨域问题解决

    VUE是基于node.js,所以解决跨域问题,设置一下反向代理即可. 我这里要调用的第三方接口地址为 http://v.juhe.cn/toutiao/index?type=top&key=1 ...

  3. LIS问题

    LIS定义LIS(Longest Increasing Subsequence)最长上升子序列 .一个数的序列bi,当b1 < b2 < … < bS的时候,我们称这个序列是上升的. ...

  4. 字符串替换 (replace)

    将文本文件中指定的字符串替换成新字符串. 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束.end后面有两个字符串,要求用第二个字符串替 ...

  5. IDEA代码检验出错

    该软件功能过于强大,会自动检验执行所需要的代码,所以会报错,但实际上我们有写 例如 解决方法 File-->setting 将error改为warning

  6. Kubernetes 各版本镜像列表

    以下镜像列表由 kubeadm v1.11.1 导出,若使用预下载镜像离线部署的方式部署,请使用 kubeadm v1.11.1 版本 导出各版本镜像列表: kubeadm config images ...

  7. main方法

    main函数的分析(python) 对于很多编程语言来说,程序都必须要有一个入口,比如C,C++,以及完全面向对象的编程语言Java,C#等.如果你接触过这些语言,对于程序入口这个概念应该很好理解,C ...

  8. springCloud 之 Eureka服务治理机制及代码运行

    服务提供者 服务注册: 服务提供者在启动的时候通过发送Rest请求的方式将自己注册到Eureka Server上,同时带上了自身服务的一些元数据信息.Eureka Server在收到这个请求后,将元数 ...

  9. R 《回归分析与线性统计模型》page141,5.2

    rm(list = ls()) library(car) library(MASS) library(openxlsx) A = read.xlsx("data141.xlsx") ...

  10. Java从.CSV文件中读取数据和写入

    .CSV文件是以逗号分割的数据仓储,读取数据时从每一行中读取一条数据元祖,也就是一条数据,再用字符分割的方式获取表中的每一个数据项. import java.io.BufferedReader;    ...