我正在使用带有实时数据的房间。 当我从视图模型中进行选择时,我看不到任何东西。 但是当我直接选择道时,我可以看到项目。 怎么了?
房间没有可变的实时数据,所以我选择了 livadata...但它不起作用。 请帮助我。 我不明白我的问题。
当我使用 debugImplementation 'com.amitshekhar.android:debug-db:1.0.6' 输入"http://localhost:8080"并搜索结果查询时,结果是正确的。 我认为问题是使用实时数据。 我登录了视图模型,这是空的。
@Dao
public interface MemoDao {
@Query("select * from memolist where date between :fromDate and :toDate and isDeleted='true' order by date")
LiveData<List<MemoEntity>> selectAll(String fromDate, String toDate);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertMemo(MemoEntity memo);
@Update
void updateMemo(MemoEntity memo);
@Delete
void deleteMemo(MemoEntity... memo);
}
@Database(entities = {MemoEntity.class}, version = 2)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase appDatabase;
public abstract MemoDao MemoDao();
public static AppDatabase getInstance(Context context){
appDatabase = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "memolist")
.addMigrations(MIGRATION_1_2)
.build();
return appDatabase;
}
static final Migration MIGRATION_1_2= new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE memolist ADD COLUMN id INTEGER NOT NULL DEFAULT 0");
}
};
}
public class MemoViewModel extends ViewModel {
LiveData<List<MemoEntity>> memoModel;
AppDatabase mRepository;
public void init(Context context,String fromDate, String toDate){
mRepository = AppDatabase.getInstance(context);
memoModel = mRepository.MemoDao().selectAll(fromDate,toDate);
}
public LiveData<List<MemoEntity>> memoList(){
Log.d("TAG",memoModel.getValue().get(0).getId()+"");
return memoModel;
}
public LiveData<List<MemoEntity>> memoListByTag(String tag, String fromDate, String toDate){
memoModel = mRepository.MemoDao().selectAllByTag(tag,fromDate,toDate);
return memoModel;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fromDate = (String)dataHolder.popDataHolder("fromDate");
toDate = (String)dataHolder.popDataHolder("toDate");
memoViewModel = ViewModelProviders.of(this).get(MemoViewModel.class);
memoViewModel.init(this.getContext(),fromDate,toDate);
memoViewModel.memoList().observe(this, new Observer<List<MemoEntity>>(){
@Override
public void onChanged(List<MemoEntity> memoEntities) {
listItems = memoViewModel.memoList().getValue();
recyclerViewAdapter.notifyDataSetChanged();
}
});
}
像这样更改您的 MemoViewModel 类
public class MemoViewModel extends ViewModel {
MutableLiveData<List<MemoEntity>> memoModel=new MutableLiveData();
AppDatabase mRepository;
public void init(Context context,String fromDate, String toDate){
mRepository = AppDatabase.getInstance(context);
memoModel.postValue(mRepository.MemoDao().selectAll(fromDate,toDate))
}
public MutableLiveData<List<MemoEntity>> memoList(){
return memoModel;
}
public LiveData<List<MemoEntity>> memoListByTag(String tag, String fromDate, String toDate){
memoModel = mRepository.MemoDao().selectAllByTag(tag,fromDate,toDate);
return memoModel;
}
}
改变你的 DAO 类,就像
@Dao
public interface MemoDao {
@Query("select * from memolist where date between :fromDate and :toDate and isDeleted='true' order by date")
List<MemoEntity> selectAll(String fromDate, String toDate);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertMemo(MemoEntity memo);
@Update
void updateMemo(MemoEntity memo);
@Delete
void deleteMemo(MemoEntity... memo);
}
MutableLiveData 是 LiveData 的一个子类,用于其某些属性(setValue/postValue(,使用这些属性,我们可以在调用 onChange(( 时轻松通知 UI。