我正在尝试从Firebase将数据加载到RecycleView。但我得到转换错误。
com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type uk.co.stableweb.kithunugee.model.Hymn
at com.google.android.gms.internal.zzaix.zzd(Unknown Source)
at com.google.android.gms.internal.zzaix.zzb(Unknown Source)
at com.google.android.gms.internal.zzaix.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:136)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:176)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2625)
at android.view.View.measure(View.java:18850)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5956)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1192)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:733)
at android.view.View.measure(View.java:18850)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5956)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5956)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5956)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5956)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18850)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5956)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2671)
at android.view.View.measure(View.java:18850)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2107)
这是我从RecycleView加载数据的方式。
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Hymn, HymnHolder>(Hymn.class, R.layout.hymn_row, HymnHolder.class, ref) {
@Override
protected void populateViewHolder(HymnHolder viewHolder, Hymn hymn, int position) {
viewHolder.setTitle(hymn.getTitle());
viewHolder.setIndex(String.valueOf(hymn.getIndex()));
}
};
// Scroll to bottom on new messages
mRecyclerViewAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
mLayoutManager.smoothScrollToPosition(recyclerView, null, mRecyclerViewAdapter.getItemCount());
}
});
recyclerView.setAdapter(mRecyclerViewAdapter);
我的ViewHolder类是,
public class HymnHolder extends RecyclerView.ViewHolder {
View mView;
public HymnHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String name) {
TextView field = (TextView) mView.findViewById(R.id.title);
field.setText(name);
}
public void setIndex(String index) {
TextView field = (TextView) mView.findViewById(R.id.index);
field.setText(index);
}
}
这是我的模特。
public class Hymn {
private int index;
private String title;
private String content;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
以下是我的Firebase数据库的结构。
我参考了this Firebase用户界面教程。以前关于这个问题的问题很少,但我不清楚。如果您对此问题有任何暗示,那将非常有用。
答案 0 :(得分:1)
更改数据库参考
旧代码
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
新代码
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("geethika");