![](/img/trans.png)
[英]Android room how to increment count field and updating specific row fields
[英]Android: Getting the count of items with a specific code in a Room database
我實質上是在嘗試創建某種搜索功能。 我正在使用Room數據庫,並且每個元素都有用戶提交的特定3位代碼。 但是,當我嘗試獲取具有特定3位代碼的元素數並將其顯示在“主要活動”中的Toast消息中時,結果總是為0。
表的每個實體都是我創建的Word類的對象,如下所示:
@Entity(tableName = "word_table")
public class Word {
@PrimaryKey(autoGenerate = true)
@NonNull
private int uid;
@ColumnInfo(name = "word_code")
private int wordCode;
@ColumnInfo(name = "word")
private String word;
public Word(int uid, int wordCode, String word){
this.uid = uid;
this.wordCode = wordCode;
this.word = word;
}
public int getUid(){
return uid;
}
public int getWordCode(){
return wordCode;
}
public String getWord(){
return word;
}
}
我的DAO類如下所示:
@Dao
public interface WordDao {
@Insert
void insert(Word word);
@Query("SELECT COUNT(*) FROM word_table WHERE word = :selectedCode")
LiveData<Integer> getWordWithCode(int selectedCode);
}
我擴展RoomDatabase的類如下所示:
@Database(entities = {Word.class}, version = 1, exportSchema = false)
public abstract class WordRoomDatabase extends RoomDatabase {
public abstract WordDao wordDao();
private static WordRoomDatabase INSTANCE;
public static WordRoomDatabase getDatabase(final Context context){
if (INSTANCE == null){
synchronized (WordRoomDatabase.class){
if (INSTANCE==null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), WordRoomDatabase.class, "word_database")
.fallbackToDestructiveMigration()
.addCallback(sDatabasePopulateCallback)
.build();
}
}
}
return INSTANCE;
}
private static RoomDatabase.Callback sDatabasePopulateCallback = new RoomDatabase.Callback(){
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
new PopulateDatabaseAsync(INSTANCE).execute();
}
};
private static class PopulateDatabaseAsync extends AsyncTask<Void, Void, Void> {
private WordDao mWordDao;
PopulateDatabaseAsync(WordRoomDatabase db){
mWordDao = db.wordDao();
}
int[] discCodes = {232, 432, 523,233,242,554,221};
String[] discNames = {"First Word", "Second Word", "Third Word", "Fourth Word", "Fifth Word", "Sixth word", "Seventh Word"};
@Override
protected Void doInBackground(Void... voids) {
for (int i = 0;i < discCodes.length; i++){
Word newWord = new Word(0, discCodes[i], discNames[i]);
mWordDao.insert(newWord);
}
return null;
}
}
}
我正在使用discCodes和discNames數組來填充列表作為示例。
這是我的存儲庫的外觀:
public class WordRepository {
private WordDao mWordDao;
private LiveData<Integer> mCount;
int thiscount;
WordRepository(Application application){
WordRoomDatabase db = WordRoomDatabase.getDatabase(application);
mWordDao = db.wordDao();
}
public void insert(Word word){
new insertWordAsync(mWordDao).execute(word);
}
private static class insertWordAsync extends AsyncTask<Word, Void, Void>{
private WordDao mDao;
insertWordAsync(WordDao dao){
mDao = dao;
}
@Override
protected Void doInBackground(Word... words) {
mDao.insert(words[0]);
return null;
}
}
public LiveData<Integer> getCount(int givenCode){
return mWordDao.getWordWithCode(givenCode);
}
}
這是ViewModel類:
public class WordViewModel extends AndroidViewModel {
private WordRepository mRepo;
public WordViewModel(@NonNull Application application) {
super(application);
mRepo = new WordRepository(application);
}
public void insert(Word word){
mRepo.insert(word);
}
public LiveData<Integer> getCount(int givenCode){
return mRepo.getCount(givenCode);
}
}
最后是MainActivity類:
public class MainActivity extends AppCompatActivity {
private WordViewModel mWordViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWordViewModel = ViewModelProviders.of(this).get(WordViewModel.class);
mWordViewModel.getCount(242).observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
String toShow = Integer.toString(integer);
Toast.makeText(getApplicationContext(), toShow, Toast.LENGTH_SHORT).show();
}
});
}
}
現在,在此特定示例中,我將代碼242作為搜索的基礎。 我首先嘗試使用int而不是LiveData的返回類型,這意味着我的DAO查詢將如下所示:
@Query("SELECT COUNT(*) FROM word_table WHERE word = :selectedCode")
int getWordWithCode(int selectedCode);
經過一些搜索后,我發現使用LiveData和Observer是更可取的。 但是,當我運行該應用程序時,而不是Toast消息中得到1,而是得到0。
我是android開發的新手,所以我為凌亂的代碼和粘貼所有這些代碼表示歉意。 我一直在通過Google Codelabs教程學習有關Room的知識,因此,如果對Room有了更好的了解可以解決我的問題,請隨時向我建議任何學習資源。 提前致謝!
您實際上是在搜索word
而不是wordCode
在WordDao
嘗試此代碼
@Query("SELECT COUNT(*) FROM word_table WHERE word_code = :selectedCode")
LiveData<Integer> getWordWithCode
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.