Morozov
Morozov

Reputation: 5260

Display data from Firebase Realtime Database

Created next structure in my database, and now want to display data in my list

enter image description here

Tried to add next code in onViewCreated:

databaseReference = FirebaseDatabase.getInstance().reference
        val wordsRef = databaseReference?.child("talk-6e3c0")?.child("words")

        val valueEventListener = object : ValueEventListener {

            override fun onDataChange(dataSnapshot: DataSnapshot) {
                topWordsList.clear()
                for (wordsSnapshot in dataSnapshot.children) {
                    val topWord = wordsSnapshot.getValue(TopWord::class.java)
                    topWord?.let { topWordsList.add(it) }
                }
            }

            override fun onCancelled(databaseError: DatabaseError) {
                Log.d("some", "Error trying to get targets for ${databaseError.message}")
            }
        }
        wordsRef?.addListenerForSingleValueEvent(valueEventListener)

Method onDataChanged was called, but i can't get name for example.

My model:

data class TopWord(
    val name: String = "",
    val description: String = ""
)

Upvotes: 0

Views: 55

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 139029

You aren't getting something because you are adding the name of your project as a child in the reference and there is no need for that. So please change the following line of code:

val wordsRef = databaseReference?.child("talk-6e3c0")?.child("words")

to

val wordsRef = databaseReference?.child("words")

I just removed the call to .child("talk-6e3c0")?.

Upvotes: 1

Related Questions