一、说在前面
昨天 | 学习了数据库的一些简单操作 |
今天 | 使用数据库,完成对记账本的账单记录的增删 |
问题 | 没有 |
二、数据库
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() +" "; } 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(); } });