一、说在前面

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

二、数据库

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. 十二、js去掉空格_比较字符长度_中英文判断_页面初始化_简体字与繁字体判断

    1.去掉字符串前后所有空格 function trimBlank(str){ return str.replace(/(^\s*)|(\s*$)/g, ""); } 2.字符串长度 ...

  2. 搭建solr集群的时候出现 ./zkcli.sh:行13: unzip: 未找到命令

    主要的原因是: linux系统下面没有安装压缩解压工具    zip 和 unzip:需要我们自己手动的安装: 利用yum命令安装即可: yum install -y unzip zip

  3. 写的一个轻量级javascript框架的设计模式

    公司一直使用jQuery框架,一些小的项目还是觉得jQuery框架太过于强大了,于是自己周末有空琢磨着写个自己的框架.谈到js的设计模式,不得不说说js的类继承机制,javascript不同于PHP可 ...

  4. netcore OA权限管理系统

    下载

  5. SciPy 特殊函数

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  6. springMVC,spring和Hibernate整合(重要)

    springMVC,spring和Hibernate整合 https://my.oschina.net/hugohxb/blog/184715 第一步:搭建一个springmvc工程,需要的jar有: ...

  7. SqlServer查看锁表与解锁

    某些情况下,sqlserver的表会被锁住,比如某个会话窗口有数据一直没提交,窗口又没关闭,这时表就会被锁住 其他任何连接查询表数据时都不会返回 这时需要手工杀掉产生死锁的会话ID,才能恢复正常 查看 ...

  8. css滚动

    css 滚动transform: translateY(-100px);jquery $(box).height(); //获取元素高度$(box).scrollTop();//获得元素的滚动条高度

  9. Java笔记--枚举&注解

    1.自定义枚举类的实现,例: class Season{ //1,提供类的属性,声明为rivate final private final String name; private final Str ...

  10. 014、Java中byte自动转型的操作

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...