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

我目前正在使用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转换到一个列表与以下stacktrace:

12-02 10:20:01.497 22282-22282 / com.test.app E / AndroidRuntime:致命异常:主进程:com.test.app,PID:22282 com.google.firebase.database.DatabaseException:预期列表while反序列化,但com.google.android.gms.internal.zzbqi.zza(未知源)在com.google.android.gms.internal.zzbqi.zza(未知源)获得了java.util.HashMap类。 google.android.gms.internal.zzbqi.zza(未知来源)com.google.firebase.database.DataSnapshot.getValue(未知源代码)com.test.app.fragment.StatsFragment $ onRetrieveBusinessObjects $ 1.onDataChange(StatsFragment.kt :46)at com.google.firebase.database.Query $ 1.onDataChange(Unknown Source)at com.google.android.gms.internal.zzbmz.zza(Unknown Source)at com.google.android.gms.internal.zzbnz .zzYj(Unknown Source)at com.google.android.gms.internal.zzboc $ 1.run(Unknown Source)at android.os.Handler.handleCallback(Handler.java:739)at android.os.Handler.dispatchMessage(Handler .java:95)在android.os.Looper.loop(Looper.java:148)a android.app.ActivityThread.main(ActivityThread.java:5417)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)java.lang.reflect.Method.invoke(Native Method)在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数据库中的查询吗? 有没有可能检索我的数据作为一个列表,而不是一个哈希表?

感谢您的帮助 !

你需要访问onDataChange中的结果列表如下(这是在Java中,所以可以让你转换为Kotlin :))

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