VVB
VVB

Reputation: 7641

How to know which exactly new entry is added in firebase DB?

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

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?

Upvotes: 2

Views: 1148

Answers (2)

Nick Zisis
Nick Zisis

Reputation: 288

One way that compines @Frank van solution is to first use your listener

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

to retrieve all the data at once and display them on your Recycle view and then unregister your listener and add a new ChildEventListener and the next node that Firebase will send it will be the last entry.

With this solution the logic is becoming a little messy. Another solution is to add a date field in your firebase model, retrieve all data from firebase and store them in a TreeMap structure which you can use a Comparator to sort the data according to the date. And then you will have all your data ordered from the last entry to the first. From there you can write code to your Recycler View Adapter to handle the first row differently from other rows(e.g. Highlight the first row).

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598688

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.

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

A first snippet:

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

});

Upvotes: 1

Related Questions