增加了显示当月总收入和总支出的功能,增加了选择收支类型的功能,删去了删除账目后恢复的功能。
1、数据库的升级
1、entity
添加了一个收支类型的字段:
package com.example.cashbook; ? import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; ? @Entity public class Notes { @PrimaryKey(autoGenerate = true) private int id; ? @ColumnInfo(name = "money") private int money; ? @ColumnInfo(name = "money_data") private String moneyData; ? @ColumnInfo(name = "other_info") private String otherInfo; ? @ColumnInfo(name = "money_type") private String moneyType; ? public Notes() { } ? public Notes(int money, String moneyData, String otherInfo, String moneyType) { this.money = money; this.moneyData = moneyData; this.otherInfo = otherInfo; this.moneyType = moneyType; } ? public String getMoneyType() { return moneyType; } ? public void setMoneyType(String moneyType) { this.moneyType = moneyType; } ? public int getId() { return id; } ? public void setId(int id) { this.id = id; } ? public int getMoney() { return money; } ? public void setMoney(int money) { this.money = money; } ? public String getMoneyData() { return moneyData; } ? public void setMoneyData(String moneyData) { this.moneyData = moneyData; } ? public String getOtherInfo() { return otherInfo; } ? public void setOtherInfo(String otherInfo) { this.otherInfo = otherInfo; } } ?
2、dao
增加了查询所有支出和收入的金额:
package com.example.cashbook; ? import androidx.lifecycle.LiveData; 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 NotesDao { ? @Insert void insertNote(Notes... notes); ? @Update void updateNote(Notes... notes); ? @Query("select * from notes") LiveData<List<Notes>> getNotesList(); ? @Query("select * from notes where other_info like :pattern order by id desc") LiveData<List<Notes>> getNotesByPattern(String pattern); ? @Query("select money from notes where money_type = :pattern") List<Integer> getMoneyListByPattern(String pattern); ? @Delete void deleteNote(Notes... notes); } ?
3、database
为方便使用,使得数据库的操作可以在主线程中执行:
package com.example.cashbook; ? import android.content.Context; ? import androidx.annotation.NonNull; import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; import androidx.room.migration.Migration; import androidx.sqlite.db.SupportSQLiteDatabase; ? @Database(entities = {Notes.class},version = 2,exportSchema = false) public abstract class NotesDatabase extends RoomDatabase { private static NotesDatabase INSTANCE; ? static synchronized NotesDatabase getDatabase(Context context){ if (INSTANCE == null){ INSTANCE = Room.databaseBuilder(context.getApplicationContext(),NotesDatabase.class,"notes_database") .addMigrations(VERSION_1_2) .allowMainThreadQueries() .build(); } return INSTANCE; } ? public abstract NotesDao getNotesDao(); ? private static final Migration VERSION_1_2 = new Migration(1,2) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE notes ADD COLUMN money_type varchar"); } }; }
4、viewmodel
增加了获取总支出和总收入金额的方法:
package com.example.cashbook; ? import android.app.Application; ? import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; ? import java.util.List; ? public class NotesViewModel extends AndroidViewModel { private NotesRepository repository; ? public NotesViewModel(@NonNull Application application) { super(application); repository = new NotesRepository(application); } ? public void updateNote(Notes... notes){ repository.updateNote(notes); } ? public void insertNote(Notes... notes){ repository.insertNote(notes); } ? public LiveData<List<Notes>> getAllList(){ return repository.getNotesList(); } ? public LiveData<List<Notes>> getAllListByPattern(String pattern){ return repository.getNotesListByPattern(pattern); } ? public void deleteNote(Notes... notes){ repository.deleteNote(notes); } //获得总收入 public int getMoneyIn(){ List<Integer> moenyIn = repository.getMoenyIn(); int sum = 0; for (Integer integer : moenyIn) { sum += integer.intValue(); } return sum; } ? //获得总支出 public int getMoneyOut(){ List<Integer> moenyOut = repository.getMoenyOut(); int sum = 0; for (Integer integer : moenyOut) { sum += integer.intValue(); } return sum; } }
2、布局的修改
在recyclerview上方添加了一个cardview,用来显示总支出和总收入。