使用ROOM从数据库读取数据并返回LiveData时,getValue()方法将返回null。一段时间以来,我一直无法理解出了什么问题。您能协助解决这个问题吗?数据库中有数据,似乎更多是我如何使用LiveData对象的问题。
活动:
public class ExercisesViewActivity extends AppCompatActivity implements View.OnClickListener {
private ExerciseViewModel exerciseViewModel;
private ExercisesAdapter recyclerViewerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercises_view);
Toolbar toolbar = findViewById(R.id.toolbar_exercise_view_activity);
toolbar.setTitle("Exercises");
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
RecyclerView recyclerView = findViewById(R.id.exercise_view_recycle_viewer);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
this.recyclerViewerAdapter = new ExercisesAdapter();
recyclerView.setAdapter(recyclerViewerAdapter);
this.exerciseViewModel = ViewModelProviders.of(this).get(ExerciseViewModel.class);
this.exerciseViewModel.setFilters("", "");
// this.exerciseViewModel.selectAll();
this.exerciseViewModel.select().observe(this, exercises -> {
if (exercises != null) {
this.recyclerViewerAdapter.updateDataset(exercises);
}
});
Button button = findViewById(R.id.test_button);
button.setOnClickListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
@Override
public void onClick(View v) {
this.exerciseViewModel.setFilters("", "");
// this.exerciseViewModel.select().observe(this, exercises -> {
// if (exercises != null) {
// this.recyclerViewerAdapter.updateDataset(exercises);
// }
// });
}
}
ViewModel:
public class ExerciseViewModel extends AndroidViewModel {
ExercisesRepository repository;
MutableLiveData<List<Exercise>> data;
public ExerciseViewModel(Application application) {
super(application);
this.data = new MutableLiveData<>();
this.repository = new ExercisesRepository(application);
}
public void setFilters(String muscleGroups, String type) {
LiveData<List<Exercise>> listLiveData = this.repository.filterSelect(muscleGroups, type);
this.data.setValue(listLiveData.getValue());
}
public void selectAll() {
// this.data.setValue(this.repository.selectAll().getValue());
}
public LiveData<List<Exercise>> select() {
return data;
}
public void insert(Exercise exercise) {
this.repository.insert(exercise);
}
}
存储库:
public class ExercisesRepository {
private ExerciseDao dao;
public ExercisesRepository(Application context) {
WorkoutRoomDatabase database = WorkoutRoomDatabase.getDb(context);
this.dao = database.exerciseDao();
}
public LiveData<List<Exercise>> filterSelect(String muscleGroups, String type) {
return this.dao.filterSelect("%" + muscleGroups + "%", "%" + type + "%");
}
public LiveData<List<Exercise>> selectAll() {
return this.dao.selectAll();
}
public void insert(Exercise exercise) {
new insertAsyncTask(this.dao).execute(exercise);
}
private static class insertAsyncTask extends AsyncTask<Exercise, Void, Void> {
private ExerciseDao exerciseDao;
insertAsyncTask(ExerciseDao dao) {
exerciseDao = dao;
}
@Override
protected Void doInBackground(final Exercise... params) {
exerciseDao.insert(params[0]);
return null;
}
}
}
DAO:
@Dao
public interface ExerciseDao {
@Query("SELECT * FROM exercises WHERE muscleGroups LIKE :muscleGroup AND type LIKE :type")
LiveData<List<Exercise>> filterSelect(String muscleGroup, String type);
@Query("SELECT * FROM exercises")
LiveData<List<Exercise>> selectAll();
@Insert
void insert(Exercise exercise);
@Update
void update(Exercise exercise);
@Delete
void delete(Exercise exercise);
@Query("DELETE FROM exercises")
void deleteAll();
}
更新:
如果在我的viewModel中,我更改了过滤器功能以返回一个新的LiveData对象,则将获取正确的数据:
public LiveData<List<Exercise>> filterSelect(String muscleGroups, String type) {
return this.dao.filterSelect("%" + muscleGroups + "%", "%" + type + "%");
}
但是在我的活动中,我需要创建一个新的观察者,因为现在数据是由LiveData的新实例提供的:
@Override
public void onClick(View v) {
this.exerciseViewModel.setFilters("Biceps", "weight");
this.exerciseViewModel.select().observe(this, exercises -> {
if (exercises != null) {
this.recyclerViewerAdapter.updateDataset(exercises);
}
});
}
这绝对是正确的方法:/
答案 0 :(得分:2)
您应该查看MediatorLiveData而不是MutableLiveData,它可以使您从存储库中传播更改。
这看起来类似于以下内容,其中您将存储库数据作为数据源添加到mediatorLiveData。当您从存储库接收数据时,这将触发回调,然后您可以通过调用setValue从mediatorLiveData发出该数据。
$True
答案 1 :(得分:0)
您尝试使用空值在filterSelete中进行搜索。 this.exerciseViewModel.setFilters("", "");
。
这个过滤器基本上是一个问题。您不能像搜索一样做空。
不用担心,您可以使用loadAll
查询。
@Query("SELECT * FROM exercises")
LiveData<List<Exercise>> loadAll();
编辑:
public LiveData<List<Exercise>> filterSelect(String muscleGroups, String type) {
if(muscleGroups == "" && type == "") {
return this.dao.loadAll()
}else {
return this.dao.filterSelect("%" + muscleGroups + "%", "%" + type + "%");
}
希望,这将解决您的问题。
答案 2 :(得分:0)
我设法使用MediatorLiveData和Transformations解决了这个问题。
所以现在我的ViewModel构造函数看起来像这样:
public ExerciseViewModel(Application application) {
super(application);
this.repository = new ExercisesRepository(application);
this.filterMutableLiveData = new MutableLiveData<>();
ExercisesFilter filter = new ExercisesFilter();
filter.muscleGroup = "";
filter.type = "";
this.filterMutableLiveData.setValue(filter);
LiveData<List<Exercise>> source = Transformations.switchMap(
this.filterMutableLiveData, value -> repository.filterSelect(value.muscleGroup, value.type)
);
this.data.addSource(source, val -> this.data.setValue(val));
}
我添加了此方法:
public void setFilterMutableLiveData(String muscleGroups, String type) {
ExercisesFilter filter1 = new ExercisesFilter();
filter1.muscleGroup = muscleGroups;
filter1.type = type;
this.filterMutableLiveData.setValue(filter1);
}
使用这种方法,每次更新过滤器时,都会触发传递到Transformations.switchMap的函数以获取新数据。最后,将新的LiveData对象添加到MediatorLiveData对象,因此,一个观察者可以用于所有源。