无法将列表检索到Firebase数据库

时间:2016-12-02 09:30:07

标签: android firebase firebase-realtime-database

我目前正在使用Firebase数据库开展项目,但无法将数据检索为列表。

这里是我导入firebase的json示例:

{
  "mood" : [ {
    "date" : 1480625911154,
    "reasons" : "Lorem Ipsum",
    "status" : "Ok",
    "user" : "812c3772-5e3b-4776-8b4c-41ee3047011d"
  }, {
    "date" : 1480626081958,
    "reasons" : "Lorem Ipsum",
    "status" : "Ok",
    "user" : "3fca7432-ac0b-40f8-b898-929df4d3a2e8"
  }, {
    "date" : 1480626567674,
    "reasons" : "Lorem Ipsum",
    "status" : "Ok",
    "user" : "22e9254a-161b-4126-8cd2-7e5ebe03fa6f"
  } ]
}

在我的Android应用程序中,我正在尝试使用以下查询和代码检索特定用户的数据(它位于Kotlin中):

FirebaseDatabase.getInstance().getReference("mood").orderByChild("user").equalTo("fa224092-2dfa-49c4-9fb6-c63eccf61a8b").addListenerForSingleValueEvent(object : ValueEventListener
{
  override fun onCancelled(error: DatabaseError)
  {
  }

  override fun onDataChange(snapshot: DataSnapshot)
  {
    moods = snapshot.getValue(object : GenericTypeIndicator<List<Mood>>(){

    })
  }
})

其中moods声明为:private var moods = emptyList<Mood>()Mood类是(它是java):

@IgnoreExtraProperties
public final class Mood
    implements Serializable
{

  public String user;

  public String status;

  public String reasons;

  public long date;

  public Mood()
  {
    super();
  }

  public Mood(String user, String status, String reasons, long date)
  {
    this.user = user;
    this.status = status;
    this.reasons = reasons;
    this.date = date;
  }

}

每次,应用程序在DataSnapshot转换为具有以下堆栈跟踪的列表时崩溃:

  

12-02 10:20:01.497 22282-22282 / com.test.app E / AndroidRuntime:FATAL   EXCEPTION:主程序:com.test.app,PID:22282   com.google.firebase.database.DatabaseException:期望列表   反序列化,但得到了一个java.util.HashMap类       在com.google.android.gms.internal.zzbqi.zza(未知来源)       在com.google.android.gms.internal.zzbqi.zza(未知来源)       在com.google.android.gms.internal.zzbqi.zza(未知来源)       在com.google.firebase.database.DataSnapshot.getValue(未知来源)       at com.test.app.fragment.StatsFragment $ onRetrieveBusinessObjects $ 1.onDataChange(StatsFragment.kt:46)       在com.google.firebase.database.Query $ 1.onDataChange(未知来源)       在com.google.android.gms.internal.zzbmz.zza(未知来源)       在com.google.android.gms.internal.zzbnz.zzYj(未知来源)       在com.google.android.gms.internal.zzboc $ 1.run(未知来源)       在android.os.Handler.handleCallback(Handler.java:739)       在android.os.Handler.dispatchMessage(Handler.java:95)       在android.os.Looper.loop(Looper.java:148)       在android.app.ActivityThread.main(ActivityThread.java:5417)       at java.lang.reflect.Method.invoke(Native Method)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我不明白为什么要检索HashMap ...实际上,在之前的项目中,使用类似的代码,Firebase会按预期给我一个lsit。

为了进行比较,这里是前一个项目的代码(在java中)。

已导入Firebase数据库的json:

{
  "books" : [ {
    "cartoonist" : "Test1",
    "id" : "10",
    "image" : "http://static.fnac-static.com/multimedia/Images/FR/NR/22/e1/7e/8315170/1507-1.jpg",
    "price" : "15.95",
    "publication" : "2016-11-01T00:00:00+01:00",
    "summaryExtract" : "Loremp Ipsum",
    "summaryFull" : "Loremp Ipsum",
    "tome" : "Tome 24 : Le testament de William S"
  }, {
    "cartoonist" : "Riad Sattouf",
    "id" : "1",
    "image" : "http://static.fnac-static.com/multimedia/Images/FR/NR/da/9d/7c/8166874/1507-1.jpg",
    "price" : "20.90",
    "publication" : "2016-10-01T00:00:00+01:00",
    "summaryExtract" : "Loremp Ipsum",
    "summaryFull" : "Loremp Ipsum",
    "tome" : "Tome 3 : L'Arabe du futur"
  }]
}

这里是查询Firebase的代码(在java中):

FirebaseDatabase.getInstance().getReference().child("books").addListenerForSingleValueEvent(new ValueEventListener()
{
  @Override
  public void onDataChange(DataSnapshot dataSnapshot)
  {
    final List<Book> books = dataSnapshot.getValue(new GenericTypeIndicator<List<Book>>()
    {
    });
  }

  @Override
  public void onCancelled(DatabaseError databaseError)
  {
  }
});

Book类的代码:

@IgnoreExtraProperties
public final class Book
    implements Serializable
{

  public String id;

  public String title;

  public String tome;

  public String cartoonist;

  public String publication;

  public String price;

  public String image;

  public String summaryFull;

  public String summaryExtract;

  public Book()
  {
    super();
  }

  public Book(String id, String cartoonist, String image, String price, String publication, String summaryExtract,
      String summaryFull, String title, String tome)
  {
    this.id = id;
    this.cartoonist = cartoonist;
    this.image = image;
    this.price = price;
    this.publication = publication;
    this.summaryExtract = summaryExtract;
    this.summaryFull = summaryFull;
    this.title = title;
    this.tome = tome;
  }

  @Exclude
  public String getFormattedDate()
  {
    final DateTime dateTime = new DateTime(publication);
    return dateTime.monthOfYear().getAsText(Locale.getDefault()) + " " + dateTime.getYear();
  }

代码按预期工作。

在Firebase数据库中,我是否遗漏了查询内容?是否有可能将我的数据检索为列表而不是哈希图?

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您需要访问onDataChange中的结果列表,如下所示(这是Java中的,因此可以转换为Kotlin :))

                for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                    Book book = childSnapshot.getValue(Book.class);
                }