小编dem*_*ems的帖子

无法填充 ArrayList 类型的 MutableLiveData,结果始终为 null

我正在开发一个问答游戏,我想将一些 id 存储在 MutableLiveData 数组列表中。因此,我创建了一个函数来循环数据库中的所有文档并将每个 ID 添加到数组列表中。但结果总是空的。我不明白我哪里错了?

我正在使用 MVVM 结构

游戏视图模型:

class GameViewModel : ViewModel() {

// database instance
val db = FirebaseFirestore.getInstance()

// the current category
private val _category = MutableLiveData<String>()
val category: LiveData<String>
    get() = _category

// the list of questionIds of the selected category
private val _questionIdsArray = MutableLiveData<ArrayList<Long>>()
val questionIdsArray: LiveData<ArrayList<Long>>
    get() = _questionIdsArray

// the current question
private val _question = MutableLiveData<String>()
val question: LiveData<String>
    get() = _question


/**
 * Set Current Category
 */
fun …
Run Code Online (Sandbox Code Playgroud)

kotlin firebase android-studio android-livedata

1
推荐指数
1
解决办法
684
查看次数