Im trying to get the number of items that are in a list in my realtime database so that i can use the number to display how many cells there are but i can't find a way to do this as the code does not finish executing before the number is returned and i've tried to use a call back but its not working because it tells me to return a unit and i need to return a int
Here is my code:
interface GetCountCallback {
fun onCallback(list: ArrayList<String>)
}
override fun getItemCount(): Int {
var number = 0
getCount(object:GetCountCallback {
override fun onCallback(list: ArrayList<String>) {
number = list.count()
}
})
return number
}
private fun getCount(myCallback: GetCountCallback) {
val ref = dbSavedStories.child(currentUid!!)
ref.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
for (childSnapshot in snapshot.children) {
val key = childSnapshot.key
list.add(key.toString())
}
myCallback.onCallback(list)
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}