Эй, я пытался написать простую викторину в Android (Kotlin), и массив, кажется, пуст, когда я пытаюсь его использовать. Я получаю данные из базы данных Firebase, и они регистрируются в LogCat, но когда я пытаюсь получить размер массива, он говорит 0.
Любое руководство о том, что мне делать?
Вот мой код:
import android.app.ProgressDialog
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.google.firebase.database.*
import kotlinx.android.synthetic.main.activity_quiz.*
class QuizActivity : AppCompatActivity() {
var quizArray: ArrayList<QuizItem> = ArrayList()
private var database: FirebaseDatabase? = null
private var databaseReference: DatabaseReference? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz)
database = FirebaseDatabase.getInstance()
databaseReference = database!!.getReference("quiz")
var dbRef = databaseReference!!.child("mod1")
val progress = ProgressDialog(this)
progress.setMessage("Loading")
progress.isIndeterminate
var valueEventListener: ValueEventListener = object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
var contentSnapshot: DataSnapshot = p0
var contentChildren: Iterable<DataSnapshot> = contentSnapshot.children
for (content: DataSnapshot in contentChildren){
var quizItem: QuizItem = content.getValue(QuizItem::class.java)!!
Log.d("\n Received Data", "data: statement = "+quizItem.statement
+" id = "+quizItem.id
+" correctOption = "+quizItem.correctOption
+" optionA"+quizItem.optionA
+" optionB"+quizItem.optionB
+" optionC"+quizItem.optionC
+" optionD"+quizItem.optionD
+" \n ")
quizArray.add(quizItem)
Toast.makeText(applicationContext, "Item added for ID = "+quizItem.id, Toast.LENGTH_SHORT).show()
}
print(quizArray)
progress.dismiss()
Log.d("Data Received", "this@QuizActivity")
Log.d("Progress Bar", "this@QuizActivity Progress Bar Dismissed")
}
}
dbRef!!.addValueEventListener(valueEventListener)
progress.show()
Log.d("Progress Bar", "this@QuizActivity Progress Bar Sent")
Toast.makeText(applicationContext, "Array Size = "+quizArray.size, Toast.LENGTH_SHORT).show()
startQuiz()
}
private fun startQuiz(){
var total: Int = quizArray.size
Toast.makeText(applicationContext, total.toString(), Toast.LENGTH_SHORT).show()
var correct: Int = 0
var incorrect: Int = 0
var done: Int = 0
tv_quiz_total.text = total.toString()
tv_quiz_correct.text = correct.toString()
tv_quiz_incorrect.text = incorrect.toString()
tv_quiz_done.text = done.toString()
var i = 0
while (i<total){
tv_quiz_statement.text = quizArray[i].statement
tv_quiz_opA.text = quizArray[i].optionA
tv_quiz_opB.text = quizArray[i].optionB
tv_quiz_opC.text = quizArray[i].optionC
tv_quiz_opD.text = quizArray[i].optionD
tv_quiz_opA.setOnClickListener {
if (quizArray[i].correctOption == 1){
correct++
done++
tv_quiz_correct.text = correct.toString()
tv_quiz_done.text = done.toString()
i++
} else {
incorrect++
done++
i++
tv_quiz_incorrect.text = incorrect.toString()
}
}
tv_quiz_opB.setOnClickListener {
if (quizArray[i].correctOption == 2){
correct++
done++
tv_quiz_correct.text = correct.toString()
tv_quiz_done.text = done.toString()
i++
} else {
incorrect++
done++
i++
tv_quiz_incorrect.text = incorrect.toString()
}
}
tv_quiz_opC.setOnClickListener {
if (quizArray[i].correctOption == 3){
correct++
done++
tv_quiz_correct.text = correct.toString()
tv_quiz_done.text = done.toString()
i++
} else {
incorrect++
done++
i++
tv_quiz_incorrect.text = incorrect.toString()
}
}
tv_quiz_opD.setOnClickListener {
if (quizArray[i].correctOption == 4){
correct++
done++
tv_quiz_correct.text = correct.toString()
tv_quiz_done.text = done.toString()
i++
} else {
incorrect++
done++
i++
tv_quiz_incorrect.text = incorrect.toString()
}
}
}
}
}
Спасибо