Suppose I have got a list of data in the Room DB. Let the data be: setId, formId, formName. There can be multiple formId in the single setId. Let setId 1 contains 10 forms, 2 contains 5 forms.
Now what I wanna do is, extract the data from the db using the setId in the ViewModel.
Let my dao be:
@Query("SELECT * FROM form WHERE setId = :id")
LiveData<List<Form>> getAllFilledForms(int id);
How can I implement such action in ViewModel. I want to retrieve all the list of the forms where the set id is same, let 1.
Edit: ViewModel Class:
public class ListSetViewModel extends AndroidViewModel {
private final LiveData < List < FormSet >> allFormSets;
private FormDatabase formDatabase;
public ListSetViewModel(@NonNull Application application) {
super(application);
formDatabase = FormDatabase.getDatabase(application);
allFormSets = formDatabase.formSetDao().getAllFilledForms(setId);
}
public LiveData < List < FormSet >> getAllFormSets(setId) {
return allFormSets;
}
}
You need to inject your ViewModel with an ID by a Factory or Dagger2. Or you can use a public method to get data.
public LiveData<List<FormSet>> getAllFormSets(setId){
return allFormSets = formDatabase.formSetDao().getAllFilledForms(setId);
}