victiMusPrime
victiMusPrime

Reputation: 53

Add multiple items to recyclerview from FirestoreRecyclerView

I connected my app to the Firestore, and I have a String called timey which is a date that is picked (irrelevant how) and compared to the String timey in the Firestore documents.

I succeeded in making the app display the Firestore output to a TextView but if I have 2 entries with the same date, it always picks one and forgets about the other. So I decided to try to display the data using a FirebaseRecyclerAdapter, which displays the "single date entry" normally, but as soon as I select a date with 2 entries, it crashes the app. Here's the source code, any idea how I could make the RecyclerView display tens of entries with the same date, and have them all inside a RecyclerView as separate entries?

... passedArg is the date that is passed from a different layout.

static String passedArg;
RecyclerView reserveList;
private static final CollectionReference reservationCollection = FirebaseFirestore.getInstance().collection("reservations");
private static Query reservationsQuery;

private FirebaseFirestore db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page);
    textView = findViewById(R.id.textView);
    textView2 = findViewById(R.id.textView2);
    textView3 = findViewById(R.id.textView3);
    passedArg = getIntent().getExtras().getString(MESSAGE_KEY);
    textView.setText(passedArg);
    reservationsQuery = reservationCollection.whereEqualTo("timey", passedArg);
    RecyclerView reserveList = (RecyclerView) findViewById(R.id.reserveList);
    reserveList.setLayoutManager(new LinearLayoutManager (this));
    FirestoreRecyclerOptions<Reservations> options =
            new FirestoreRecyclerOptions.Builder<Reservations>()
                    .setQuery(reservationsQuery, Reservations.class)
                    .setLifecycleOwner(this)
                    .build();
    FirestoreRecyclerAdapter<Reservations, ReservationsHolder> adapter = new FirestoreRecyclerAdapter<Reservations, ReservationsHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull ReservationsHolder holder, int position, @NonNull Reservations model) {
            holder.setName(model.getName());
            holder.setTime(model.getTime());
            holder.setCountry(model.getCountry());
            }


        @NonNull
        @Override
        public ReservationsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ReservationsHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.reservations, parent, false));
        }
    };
        reserveList.setAdapter(adapter);

If you need any further code, let me know.

EDIT: Crash logs:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.nedim.calendar1, PID: 10342
              java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.util.Date to String (found in field 'time')
                  at com.google.android.gms.internal.firebase-firestore.zzko.zzb(Unknown Source)
                  at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
                  at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
                  at com.google.android.gms.internal.firebase-firestore.zzko.zzb(Unknown Source)
                  at com.google.android.gms.internal.firebase-firestore.zzko$zza.zza(Unknown Source)
                  at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
                  at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
                  at com.google.firebase.firestore.DocumentSnapshot.zza(Unknown Source)
                  at com.google.firebase.firestore.QueryDocumentSnapshot.zza(Unknown Source)
                  at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source)
                  at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(Unknown Source)
                  at com.firebase.ui.firestore.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:23)
                  at com.firebase.ui.firestore.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:12)
                  at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
                  at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
                  at com.firebase.ui.firestore.FirestoreRecyclerAdapter.getItem(FirestoreRecyclerAdapter.java:83)
                  at com.firebase.ui.firestore.FirestoreRecyclerAdapter.onBindViewHolder(FirestoreRecyclerAdapter.java:125)
                  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
                  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
                  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
                  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
                  at android.view.View.layout(View.java:17572)
                  at android.view.ViewGroup.layout(ViewGroup.java:5656)
                  at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
                  at android.view.View.layout(View.java:17572)
                  at android.view.ViewGroup.layout(ViewGroup.java:5656)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at android.view.View.layout(View.java:17572)
                  at android.view.ViewGroup.layout(ViewGroup.java:5656)
                  at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
                  at android.view.View.layout(View.java:17572)
                  at android.view.ViewGroup.layout(ViewGroup.java:5656)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at android.view.View.layout(View.java:17572)
                  at android.view.ViewGroup.layout(ViewGroup.java:5656)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                  at android.view.View.layout(View.java:17572)
                  at android.view.ViewGroup.layout(ViewGroup.java:5656)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at com.android.internal.policy.DecorView.onLayout(DecorView.java:755)
                  at android.view.View.layout(View.java:17572)
                  at android.view.ViewGroup.layout(ViewGroup.java:5656)
                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2429)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2149)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1306)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6579)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                  at android.view.Choreographer.doFrame(Choreographer.java:619)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6316)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
W/Firestore: (0.6.6-dev) [OnlineStateTracker]: Could not reach Firestore backend.

Upvotes: 2

Views: 483

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

You are getting the following error:

java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.util.Date to String (found in field 'time')

Because in your Firestore database, you have a property named time which is of type Date and not String. So to solve this, please see how you can store the server timestamp in a Firestore database and how you can correctly format it.

If you are interested, this is how you can correctly retrieve data a from Cloud Firestore database and display it in a RecyclerView using FirestoreRecyclerAdapter.

Upvotes: 1

Related Questions