Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't convert object of type java.util.ArrayList to type model

I'm trying to load data to RecycleView from Firebase. But I get convert error.

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)

This is how I load data from 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);

And my ViewHolder class is,

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);
    }
}

And this is my model.

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;
    }

}

Here is the structure of my Firebase database.

enter image description here

I refered this Firebase UI tutorial. There were few previous questions regarding this problem, but it's not clear to me. If you have any hint on this issue, it would be really helpful.

like image 255
Isuru Avatar asked Oct 31 '22 01:10

Isuru


1 Answers

Change your database reference

old code

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

new code

 mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("geethik‌​a");
like image 87
Rajendra Verma Avatar answered Nov 13 '22 00:11

Rajendra Verma