【问题标题】:How to know which exactly new entry is added in firebase DB?如何知道在firebase DB中添加了哪个新条目?
【发布时间】:2018-02-28 04:58:33
【问题描述】:

我的应用分为 2 个部分,用户和管理员。管理员可以在 firebase db/node 中添加/删除新项目,这些项目会反映到用户应用程序中。考虑一下,用户应用程序一直处于打开状态并使用以下代码更新列表。

mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        arrayList.clear();

        for (DataSnapshot dataSnapshotChild : dataSnapshot.getChildren()) {
            for (DataSnapshot snapshot : dataSnapshotChild.getChildren()) {
                Log.v("onDataChange", ":" + snapshot.getValue());
                Model model = new Model();

                model.setReceivedServiceCategory(dataSnapshotChild.getKey());
                model.setKey(snapshot.getKey());
                model.setReceivedFrom((String) snapshot.child("xxxxx").getValue());
                model.setRaisedAtTimings((String) snapshot.child("xxxxx").getValue());
                model.setReceivedStatus((String) snapshot.child("xxxx").getValue());
                arrayList.add(model);
            }
        }
        loadDataToRecyclerView();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.v("DatabaseError", databaseError.getMessage());
    }
});

我的问题,我想针对 firebase db/node 中新添加的条目显示弹出窗口。现在,我可以看到更新的列表,但同时我想突出显示最新添加的条目。另外,我不想比较旧列表和最新列表,如果firebase 提供任何其他合适的方式,请提供解决方案,除了比较 2 个列表?

【问题讨论】:

    标签: android firebase firebase-realtime-database firebase-authentication firebase-storage


    【解决方案1】:

    听起来您可能希望从数据库中获取有关更新数据的更详细信息。在这种情况下,您应该使用 ChildEventListener 类。鉴于树中有两个单独的分支,您需要为每个品牌使用单独的 ChildEventListener

    这会导致两个侦听器,每个侦听器都有onChildAddedonChildChanged 等的方法。但是每个方法都非常简单。

    第一个sn-p:

    mFirebaseDatabase.getChild("user").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
            Log.v("onChildAdded", ":" + snapshot.getKey() + " " + snapshot.getValue());
            Model model = new Model();
            // TODO: model.setReceivedServiceCategory(dataSnapshotChild.getKey());
            model.setKey(snapshot.getKey());
            model.setReceivedFrom((String) snapshot.child("xxxxx").getValue());
            model.setRaisedAtTimings((String) snapshot.child("xxxxx").getValue());
            model.setReceivedStatus((String) snapshot.child("xxxx").getValue());
            arrayList.add(model);
        }
        public void onChildRemoved(DataSnapshot snapshot) {
            // TODO: remove the item with snapshot.getKey() from the array list
        }
        ...
    
    });
    

    【讨论】:

    • 感谢您的回复。试试看
    • 好像只有第一次调用
    • ChildEventListener 中有四个方法在子节点生命周期中的不同时刻被调用:然后它被添加、更改、删​​除,或者当它移动到不同的位置时它的集合/查询。
    • 是的,我明白了。但就我而言,它正在调用任何方法
    • 我认为问题在于单个节点,如果里面有多个子节点/子节点,我该怎么办?我的意思是可以省略子节点并在父节点上应用
    【解决方案2】:

    组合@Frank van 解决方案的一种方法是首先使用您的监听器

     mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            arrayList.clear();
    
            for (DataSnapshot dataSnapshotChild : dataSnapshot.getChildren()) {
                for (DataSnapshot snapshot : dataSnapshotChild.getChildren()) {
                    Log.v("onDataChange", ":" + snapshot.getValue());
                    Model model = new Model();
                    model.setReceivedServiceCategory(dataSnapshotChild.getKey());
                    model.setKey(snapshot.getKey());
                    model.setReceivedFrom((String) snapshot.child("xxxxx").getValue());
                    model.setRaisedAtTimings((String) snapshot.child("xxxxx").getValue());
                    model.setReceivedStatus((String) snapshot.child("xxxx").getValue());
                    arrayList.add(model);
                }
            }
            loadDataToRecyclerView();
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.v("DatabaseError", databaseError.getMessage());
        }
    });
    

    一次检索所有数据并将它们显示在您的回收视图上,然后取消注册您的侦听器并添加一个新的 ChildEventListener,Firebase 将发送它的下一个节点将是最后一个条目。

    使用此解决方案,逻辑变得有点混乱。另一种解决方案是在您的 firebase 模型中添加一个日期字段,从 firebase 检索所有数据并将它们存储在 TreeMap 结构中,您可以使用 Comparator 根据日期对数据进行排序。然后,您将拥有从最后一个条目到第一个条目的所有数据。从那里你可以编写代码到你的 Recycler View Adapter 来处理与其他行不同的第一行(例如,突出显示第一行)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-10
      • 2017-11-03
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      相关资源
      最近更新 更多