Skip to content

Instantly share code, notes, and snippets.

@tmaxxdd
Created May 22, 2019 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmaxxdd/bdc237f67e0a06aed43cd96224ad9171 to your computer and use it in GitHub Desktop.
Save tmaxxdd/bdc237f67e0a06aed43cd96224ad9171 to your computer and use it in GitHub Desktop.
//Get path to data according to selected tab
val databaseRef = FirebaseDatabase.getInstance().getReference(ref)
val topics = ArrayList<Topic>()
databaseRef.orderByChild("index").addValueEventListener(object : ValueEventListener {
override fun onDataChange(data: DataSnapshot) {
/*
In the database a child contains more fields
than Topic model. So we have to choose which one we want to match.
*/
data.children.forEach {
val item = Topic(
it.child("name").value?.toString(),
it.child("image").value?.toString(),
it.child("description").value?.toString()
)
topics.add(item)
}
callback.onSuccess(topics)
}
override fun onCancelled(error: DatabaseError) {
error.let {
callback.onFailure(error.toException().fillInStackTrace())
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment