问题描述
测验应用程序,类别有问题,问题有答案。
我在DAO中有两个查询。
在第一个中,我从数据中获得所有类别:
@Query("SELECT * from category_table ORDER BY category_id")
fun getAllCategories(): LiveData<List<Category>>
在第二个中,我得到了按类别ID列出的问题和答案的列表:
@Query("SELECT * FROM question_table WHERE parent_category_id = :categoryId ")
fun getQuestionsWithAnswersByCategoryId(categoryId: Long): LiveData<List<QuestionWithAnswers>>
存储库:
val getAllCategories: LiveData<List<Category>> = quizDao.getAllCategories()
fun getQuestionsWithAnswersByCategoryId(id: Long): LiveData<List<QuestionWithAnswers>> {
return quizDao.getQuestionWithAnswers(id)
}
这是我的视图模型:
val getAllCategories: LiveData<List<Category>>
var questionById: LiveData<List<QuestionWithAnswers>>
init {
getAllCategories = repository.getAllCategories
questionByCategoryId = repository.getQuestionWithAnswersByCategoryId(???????)
}
问题是我事先不知道类别的ID,因此需要从数据库中获取它们。
当我尝试在这样的视图模型中获取类别ID时:
var categoryId = getAllCategories.value[0].categoryId
它返回null。
有没有办法获取包裹在viewmodel内部livedata中的类别ID?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)