简体   繁体   English

如何从 Firebase 检索不同对象类型的列表?

[英]How to retrieve list of different object types from Firebase?

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)".我面临一个问题,我有一个不同对象类型的列表,我正在使用 AddClildEventLitener() 获取该列表,所以问题是检索那些我应该将特定对象提供给“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

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. getValue(SpecificObject.class)方法将DataSnapshot的属性映射到SpecificObject类的实例。

If you have multiple different object types in your database, you will:如果您的数据库中有多个不同的对象类型,您将:

  1. Have a separate Java/Kotlin class for each object type.每个对象类型都有一个单独的 Java/Kotlin 类。
  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.使用该类型的正确类调用getValue(...)

Also see:另见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM