从FirestoreRecyclerView

时间:2018-04-30 00:11:34

标签: java android firebase android-recyclerview google-cloud-firestore

我将我的应用程序连接到Firestore,并且我有一个名为timey的字符串,这是一个被选中的日期(无关的方式),并与Firestore文档中的字符串timey进行比较。

我成功地让应用程序将Firestore输出显示为TextView,但如果我有2个具有相同日期的条目,则它总是选择一个并忘记另一个。所以我决定尝试使用FirebaseRecyclerAdapter显示数据,显示"单日期条目"通常情况下,但只要我选择包含2个条目的日期,它就会崩溃应用程序。这里是源代码,任何想法如何让RecyclerView显示数十个具有相同日期的条目,并将它们作为单独的条目放在RecyclerView中?

... passedArg是从不同布局传递的日期。

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

如果您需要更多代码,请与我们联系。

编辑:崩溃日志:

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.

1 个答案:

答案 0 :(得分:1)

您收到以下错误:

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

因为在您的Firestore数据库中,您有一个名为time的属性,其类型为Date而不是String。因此,要解决此问题,请参阅how you can store the server timestamp in a Firestore database以及如何正确format

如果您有兴趣, this 是如何从Cloud Firestore数据库正确检索数据并使用RecyclerViewFirestoreRecyclerAdapter中显示的。 / p>