1

I'm trying to retrieve a list of object "Game" from my firebase database using Kotlin on Android Studio.

my firebase database

I am using the code below:

private fun getGamesAvailable() {
    var ref = FirebaseDatabase.getInstance().getReference("games")
    var gamesMutableList: MutableList<Game> = mutableListOf()
    val menuListener = object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            for (dataValues in dataSnapshot.children) {
                val game: Game? = dataValues.getValue(Game::class.java)
                gamesMutableList.add(game!!)
            }
            val adapter = GameAdapter(applicationContext, R.layout.games_list, gamesMutableList)
            listViewGames.adapter = adapter
        }
        override fun onCancelled(databaseError: DatabaseError) {
            // handle error
        }
    }
    ref.addListenerForSingleValueEvent(menuListener)
}

but I get the error

com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap

I understand that the problem is I am trying to retrieve a list of Games that has a list of Players, but I don't know how to solve this. I found some posts about it on stackoverflow but I don't really understand the answers and how they solve this problem.

Android-Expected a List while deserializing, but got a class java.util.HashMap

Expected a List while deserializing, but got a class java.util.HashMap

If anyone could help me understand I'll be very thankfull !

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

It's hard to be certain without seeing Game. But my guess it that you've modeled mplayers as a List<Player> or Player[]. Where Player would be the class that models the data for each player under mplayers.

Firebase has a very strict definition of what an array/list looks like, and player names as the keys does not meet that definition.

What you're showing for mplayers in the JSON would translate to Map<String, Player> in your code.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi Frank, The issue was indeed my list of players. I was trying to retrieve it as a List while firebase was sending it as an HashMap. Thank you so much for your help ! – Guillaume Biquet Feb 14 '20 at 21:53
0

i hope you have not solved this issue by the moment i post my "answer", wink wink.

I assume you get the error on this line

val game: Game? = dataValues.getValue(Game::class.java)

What always helps me in this cases is to place a breakpoint where i get the data from the service the use the Evaluate Expression (Alt+F8) to see the hidden claseess the object gives back, and undestand better what is inside the box.

You may not be able to parse it directly to your Game class at the moment but by looking inside the object you may be able to get the portions of the info that you need ,then you may find easier to parse that portion of the data.

I have not used DataSnapshots , yet but

Let me give you an example with a DocumentSnapshot

Evaluate Expression

Then you can search what i has inside and parse the data one by one or by other methods you may now.

fields

That usually helps me when i don't find an easy answer online or i don't have the necesary models to parse the data.

I hope i have been of help Guillaume, Have a nice day, and a proper solution to you issue.

malta59
  • 97
  • 1
  • 9
  • 1
    Hey malta, this is a very intersting feature I did not know about. It helped me clarify Frank's answer and what I was doing wrong. I'll surely use it many more times to figure out futur problems :) Thank you ! – Guillaume Biquet Feb 14 '20 at 21:57