我想点击一个按钮,它会从 Firebase 中提取一些数据,然后我想转到下一个 Activity ,传递一个包含 Firebase 数据的数组。我仍然无法理解异步代码,我希望有人能用最简单的方法帮助我完成这个任务。
我尝试过运行阻塞、挂起函数、等待和异步,但我似乎无法正确处理。这是我的点击监听器:
btn_games_quiz.setOnClickListener {
listenForQuestions()
val intent = Intent(this, QuizActivity::class.java)
intent.putExtra("questionArray", questionArray)
startActivity(intent)
}
这里是 listenForQuestions:
private fun listenForQuestions(){
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("MM")
val formatted = current.format(formatter)
val currentMonth = QuizActivity.months[formatted.toInt()]
val ref = FirebaseDatabase.getInstance().getReference("/quizzes/$currentMonth")
ref.addChildEventListener(object: ChildEventListener {
override fun onChildAdded(p0: DataSnapshot, p1: String?) {
val questionItem = p0.getValue(Question::class.java)
if (questionItem != null) {
questionArray += questionItem
}
}
override fun onCancelled(p0: DatabaseError) {
}
override fun onChildChanged(p0: DataSnapshot, p1: String?) {
}
override fun onChildMoved(p0: DataSnapshot, p1: String?) {
}
override fun onChildRemoved(p0: DataSnapshot) {
}
})
}
如能指出正确方向,我们将不胜感激。
最佳答案
尝试使用线程。 新的Runnable();
关于android - 在移动到下一个 Activity 之前完整地运行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55463395/