如何从android中的firebase获取对象?

Posted

技术标签:

【中文标题】如何从android中的firebase获取对象?【英文标题】:how to get the object from firebase in android? 【发布时间】:2016-07-10 16:26:40 【问题描述】:

我有这样的用户类

public class User
int id;
String description;
String shortDescription;
String photo;

public int getId() 
    return id;


public void setId(int id) 
    this.id = id;


public String getDescription() 
    return description;


public void setDescription(String description) 
    this.description = description;


public String getShortDescription() 
    return shortDescription;


public void setShortDescription(String shortDescription) 
    this.shortDescription = shortDescription;


public String getPhoto() 
    return photo;


public void setPhoto(String photo) 
    this.photo = photo;


我正在使用 FirebaseRecyclerAdapter 获取存储在我的 firebase 上的用户,如下所示

 FirebaseRecyclerAdapter<User, UserHolder> adapter =
            new FirebaseRecyclerAdapter<User, UserHolder>(
                    User.class,
                    android.R.layout.two_line_list_item,
                    UserHolder.class,
                    mRef) 
                @Override
                protected void populateViewHolder(UserHolder userHolder, User u, int i) 
                    userHolder.mText.setText(u.getDescription());
                
            ;

但问题是当用户对象的 firebase 键是这样的时

 > -KDY9Pna7XqgFkyCHKFJ

那么它工作正常但是如果firebase key就像

 > c5755a68-69a7-4464-956e-c2f94d9b387d

这个 unique id 那么它给出了一个 error

> com.firebase.client.FirebaseException: Failed to bounce to type
FATAL EXCEPTION: 
main Process: in.rofr, PID: 12399
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161)
at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150)
at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5465)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5498)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4735)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4611)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3026)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2903)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1183)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2116)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1873)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1084)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5990)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)

确切的问题是什么,我没有得到任何人可以帮助我吗?

提前致谢!!!

【问题讨论】:

我能知道这一行中的 s 是什么吗userHolder.mText.setText(s.getDescription()); 我找不到你在哪里声明那个。 对不起它的 u.getDescription() 还是有点困惑,你能说说key。调试时是firebase中的那个键还是Object的键? 那么问题是 JSON 属性没有正确映射请看这个Answer 如果没有在 JSON 中看到用户(作为文本,请不要截图)和 完整 堆栈跟踪(嵌套了更有趣的错误消息),将无法提供帮助。但是@Kathi 链接的答案确实是一个好的开始。 :-) 【参考方案1】:

尝试像这样将@JsonIgnoreProperties(ignoreUnknown = true) 放在类的顶部

@JsonIgnoreProperties(ignoreUnknown = true)
public class UserModel 


这可能会起作用,因为 Firebase 使用 Jackson 库来序列化/反序列化 JSON,并且使用 @JsonIgnoreProperties(ignoreUnknown = true) 是为了忽略新属性(未在类中声明的属性),如此处所述https://***.com/a/5455563/2331705

【讨论】:

以上是关于如何从android中的firebase获取对象?的主要内容,如果未能解决你的问题,请参考以下文章

如何从android中的firebase中检索List对象

如何从 Firebase Android Query 中的 StartAt 特定位置和 EndAt 特定位置获取值

如何在 Android Studio 中使用 Kotlin 从 Firebase 中的数据库中获取特定值?

如何从 iOS 中的 Firebase 消息通知中获取主题?

如何从 Firebase android kotlin 获取所有具有特定价值的孩子?

如何从 Android 上的 Firebase DataSnapshot 中提取对象列表