【问题标题】:Reading from Firebase to recyclerview gives duplicate data从 Firebase 读取到 recyclerview 会提供重复数据
【发布时间】:2022-01-18 00:25:38
【问题描述】:

我正在尝试将数据从 firebase 读取到 recyclerview 和其中的另一个 recyclerview。第一个 recyclerview 正在获取数据,但子 recyclerview 的数据被重复。 (我有点不知道怎么解释)

我认为问题就在这里,但我不知道如何解决它。请问有人可以帮忙吗?

private void getData() {
    DatabaseReference eventRef = rootRef.child("events");

    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot ds : snapshot.getChildren()) {

                String event_date_month = ds
                        .child("event_date_month")
                        .getValue().toString();

                String event_e_time = ds
                        .child("event_e_time")
                        .getValue().toString();

                String event_location = ds
                        .child("event_location")
                        .getValue().toString();

                String event_name = ds
                        .child("event_name")
                        .getValue().toString();

                String event_s_time = ds
                        .child("event_s_time")
                        .getValue().toString();

                ChildEvent childEvent = new ChildEvent(event_name, event_s_time, event_e_time, event_location);
                childEventList.add(childEvent);
                parentEventList.add(new ParentEvent(event_date_month, childEventList));

                ParentEventAdapter parentEventAdapter = new ParentEventAdapter(parentEventList);
                parentRecyclerView.setAdapter(parentEventAdapter);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) { }
    };
    eventRef.addListenerForSingleValueEvent(eventListener);
}

这是 firebase 表:

这是我现在得到的:

这就是我想要的样子:

【问题讨论】:

  • 您到底想在第二个 RV 中显示什么?
  • 我试图添加事件(cardView 旁边有红色条)。顺便说一句,我找到了一个解决方案,感谢您的帮助。

标签: android firebase firebase-realtime-database android-recyclerview


【解决方案1】:

我明白了。非常简单,我只需将此处的列表更改为模型,这样在将其添加到适配器之前它就不会占用每个项目。

ChildEvent childEvent = new ChildEvent(event_name, event_s_time, event_e_time, event_location);
                            parentEventList.add(new ParentEvent(event_date_month, childEvent));

【讨论】:

    猜你喜欢
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2019-12-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多