道
@Query("SELECT COUNT(category) FROM todolist_table")
fun getAllTaskCount() : LiveData<Int>
回购
suspend fun getAllTaskCount() : LiveData<Int> {
return todoDao.getAllTaskCount()
}
视图模型
fun getAllTaskCount(){
viewModelScope.launch(Dispatchers.IO) {
repository.getAllTaskCount()
}
}
查看
val viewModel = ToDoViewModel(application = LocalContext.current.applicationContext as Application)
val taskCount = viewModel.getAllTaskCounter()
我想从数据库中计算类别,但它返回 null