gpt4 book ai didi

android - 无法将 java.util.ArrayList 类型的对象转换为类型模型

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:00 25 4
gpt4 key购买 nike

我正在尝试将数据从 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 数据库的结构。

enter image description here

我引用了 this Firebase 用户界面教程。以前关于这个问题的问题很少,但我不清楚。如果您对这个问题有任何提示,那将非常有帮助。

最佳答案

更改数据库引用

旧代码

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

新代码

 mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("geethik‌​a");

关于android - 无法将 java.util.ArrayList 类型的对象转换为类型模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com