今天继续了记账本代码的编写:将数据库还有仓库的写成
Word
package com.eb.writeaccount; import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; @Entity public class Word { @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name="money") private String money; @ColumnInfo(name = "atime") private String atime; public Word(String money, String atime) { this.money = money; this.atime = atime; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } public String getAtime() { return atime; } public void setAtime(String atime) { this.atime = atime; } }
WordDao
package com.eb.writeaccount; 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 WordDao { @Insert void insertWords(Word...words); @Update void updateWords(Word...words); @Delete void deleteWords(Word...words); @Query("DELETE FROM WORD") void deleteAllWords(); @Query("SELECT * FROM WORD ORDER BY ID ASC") LiveData<List<Word>>getAllWordsLive(); @Query("SELECT * FROM WORD WHERE atime LIKE :pattern ORDER BY ID ASC") LiveData<List<Word>>findWordsWithPattern(String pattern); }
wordDatabase
package com.eb.writeaccount; import android.content.Context; import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; @Database(entities = {Word.class},version = 1,exportSchema = false) public abstract class WordDatabase extends RoomDatabase { private static WordDatabase INSTANCE; static synchronized WordDatabase getDatabase(Context context) { if (INSTANCE == null) { INSTANCE = Room.databaseBuilder(context.getApplicationContext(),WordDatabase.class,"word_database") //.fallbackToDestructiveMigration() .build(); } return INSTANCE; } public abstract WordDao getWordDao(); }
和我的仓库
package com.eb.writeaccount; import android.content.Context; import android.os.AsyncTask; import androidx.lifecycle.LiveData; import java.util.List; public class writeRepostry { private LiveData<List<Word>> allWordsLive; private WordDao wordDao; public writeRepostry(Context context) { WordDatabase wordDatabase = WordDatabase.getDatabase(context.getApplicationContext()); wordDao = wordDatabase.getWordDao(); allWordsLive = wordDao.getAllWordsLive(); } void insertWords(Word... words) { new InsertAsyncTask(wordDao).execute(words); } void updateWords(Word... words) { new UpdateAsyncTask(wordDao).execute(words); } void deleteWords(Word... words) { new DeleteAsyncTask(wordDao).execute(words); } void deleteAllWords() { new DeleteAllAsyncTask(wordDao).execute(); } LiveData<List<Word>> getAllWordsLive() { return allWordsLive; } LiveData<List<Word>> findWordsWithPattern(String pattern) { return wordDao.findWordsWithPattern("%" + pattern + "%"); } static class InsertAsyncTask extends AsyncTask<Word, Void, Void> { private WordDao wordDao; InsertAsyncTask(WordDao wordDao) { this.wordDao = wordDao; } @Override protected Void doInBackground(Word... words) { wordDao.insertWords(words); return null; } } static class UpdateAsyncTask extends AsyncTask<Word, Void, Void> { private WordDao wordDao; UpdateAsyncTask(WordDao wordDao) { this.wordDao = wordDao; } @Override protected Void doInBackground(Word... words) { wordDao.updateWords(words); return null; } } static class DeleteAsyncTask extends AsyncTask<Word, Void, Void> { private WordDao wordDao; DeleteAsyncTask(WordDao wordDao) { this.wordDao = wordDao; } @Override protected Void doInBackground(Word... words) { wordDao.deleteWords(words); return null; } } static class DeleteAllAsyncTask extends AsyncTask<Void, Void, Void> { private WordDao wordDao; DeleteAllAsyncTask(WordDao wordDao) { this.wordDao = wordDao; } @Override protected Void doInBackground(Void... voids) { wordDao.deleteAllWords(); return null; } } }