How to retrieve list of different object types from Firebase?

Khaled :

I'm facing a problem that I have a list of different object types and I'm fetching that list using AddClildEventLitener(), so the problem is to retrieve those objects I should give specific object to "getValue(SpecificObject.class)".

So, How can I achieve this step?

Example of realtime DB structure:

-List
 -random id1(Object)
  -title
  -description

 -random id2(Object)
  -first name
  -last name

 -random id3(Object)
  -title
  -image

 -random id4(Object)
  -title
  -description
  -image
Frank van Puffelen :

I'm pretty sure I've answered this not too long ago, but I'll try again.

The getValue(SpecificObject.class) method maps the properties from the DataSnapshot to an instance of the SpecificObject class.

If you have multiple different object types in your database, you will:

  1. Have a separate Java/Kotlin class for each object type.
  2. Need to include an indication of each object's class in the database.
  3. Read that indicator (typically a property) first, and then...
  4. Call getValue(...) with the correct class for that type.

Also see:

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=386147&siteId=1