如何知道在Firebase DB中添加了哪个确切的新条目? [英] How to know which exactly new entry is added in firebase DB?

本文介绍了如何知道在Firebase DB中添加了哪个确切的新条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用分为用户和管理员两部分.管理员可以在Firebase数据库/节点中添加/删除新项目,这些项目会反映到用户应用程序中.考虑一下,用户应用始终处于打开状态,并使用以下代码更新列表.

My app is divided into 2 sections, user and admin. Admin can add/delete new items in firebase db/node and which is get reflected into user app. Consider, user app is open all the time and update list with below code.

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数据库/节点中新添加的条目显示弹出窗口.现在,我可以看到更新的列表,但同时我想突出显示最新添加的条目.另外,我也不想比较旧的&最新列表,请提供我的解决方案,除非比较2个列表(如果firebase提供了其他合适的方式?

My problem, I want to display pop-up against newly added entry in firebase db/node. Simply now, I can see the updated list but same time I want to highlight the latest added entry. Also, I don't want to compare old & latest list, please provide me solution except comparing 2 lists if any other suitable way does firebase provide?

推荐答案

听起来您正在寻找从数据库中获取有关更新数据的更详细的信息.在这种情况下,您应该使用ChildEventListener类.假设您的树中有两个单独的分支,则需要为每个品牌使用单独的ChildEventListener.

It sounds likely you're looking to get more granular information about the updated data from the database. In that case you should use the ChildEventListener class. Given that you have two separate branches in your tree, you'll want to use a separate ChildEventListener for each brand.

这导致了两个侦听器,每个侦听器都有用于onChildAddedonChildChanged等的方法.但是每种方法都非常简单.

This leads to two listeners, each with methods for onChildAdded, onChildChanged, etc. But each method will be quite simple.

第一个片段:

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

});

这篇关于如何知道在Firebase DB中添加了哪个确切的新条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆