1

I can't resolve my problem.

I have an array of mappable in my Firestore database as you can see on the screen

enter image description here

Then I would like to load this list and convert all of the items in an object like this one

enter image description here

And my code to convert my list of mappable to objects is here

enter image description here

I got an error telling

java.lang.ClassCastException: java.util.HashMap cannot be cast to com.example.gestionnairedechantiers.firebase.ItemWithQuantity

The first log returns

enter image description here

I don't understand why could you help me, please? I don't find a way to convert an array of mappable to a list of objects.

Thank you very much

EDIT: A solution, maybe the best one ?

enter image description here

I convert my items from the list to HashMap, an then I convert to my Object.

1 Answer 1

2

As I see in your screenshot, the "listePersonnel" is a property of type array that contains "ItemWithQuantity" objects. When you are calling DocumentSnapshot's get(String field) method, the object that is returned is actually a HashMap and not a List, as you probably expected.

To map that array into a List, please check the following article:

Basically, you should create a new class that will hold a property called "listePersonnel" of type List and map each document into an object of that type:

val listePersonnel: List<ItemWithQuantity> = document.toObject(NewClass.class).listePersonnel

Edit:

I don't know if I can retrieve my field listePersonnel from my document as a DocumentSnapshot.

There is no way you can retrieve the "listePersonnel" and convert it to a DocumentSnapshot object. What you should do is convert the entire document into an object of the "NewClass", and then get the list, like any other property of the class.

In the article, the author uses .toObject() to convert its list to an object and you can only use it with a DocumentSnapshot.

The author (me), I'm using ".toObject()" to convert that entire document into an object that contains the desired list.

I get my document with different fields, and I want to convert a field which is a list of mappables to a list of objects.

In your database, that field is an array of objects. So you need to convert that array into a list of custom objects (as explained in this answer and that article).

I use document.get("list"), I get a Any/HashMap.

That's what we are talking about, not to use get("list") as it returns a Map, that should be iterated, as you already did, but to get that fields directly to a list of "NewClass" objects.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello Alex, thank you for the answer ! I've seen this article but in my case I didn't achieve to use it because I don't know if I can retrieve my field listePersonnel from my document as a DocumentSnapshot. In the article, the autor uses .toObject() to convert its list to an object and you can only use it with a DocumentSnapshot. In my case, I get my document with differents fields, and I want to convert a filed which is a list of mappables to a list of objects. So when I use document.get("list"), I get a Any/HashMap.
I've added a solution I found, I don't know if it's the best one
Please check my updated answer. I hope it's more clear now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.