0

When a child gets added into my firebase it Logs all the Annotation_City's in my firebase again. How can I get it to only print the new Annotation_City that got added?

public void AllLocationMarkers() {
        grabMarkersFromDb = new Firebase("***");
        grabMarkersFromDb.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot locationSnapshot : dataSnapshot.getChildren()) {
                    String city_Name = (String) locationSnapshot.child("Annotation_City").getValue();
                    Log.d("City updated", "city: " + city_Name);
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Amen Parham
  • 73
  • 1
  • 8
  • **How can i get it to only print the new Annotation_City that got added** you already got it, what you want to reach to now ? –  Jun 09 '17 at 03:23
  • Thanks for the response :). But I do not have it yet, for example. Say I have a total of one city name in my firebase names: West Covina. And I run my program it will log “West Covina”. So say I stay on my app and don’t leave it at all and I go ahead and add another city name called “Pomona” my program will then log “West Covina” and “Pomona” but I only want Pomona to be added since it’s new and West Covina is older data – Amen Parham Jun 09 '17 at 03:26
  • 1
    Firebase synchronized data, so there is no real concept of "only new data". But if you have some indication of the time in your data, you can query for data after a specific point in time. See https://stackoverflow.com/questions/24713765/firebase-android-notifications-childadded-how-to-get-only-new-childs – Frank van Puffelen Jun 09 '17 at 05:40

1 Answers1

0
grabMarkersFromDb.addChildEventListener(new ChildEventListener() {
                            @Override
                            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                                //value add then it will call
                            }

                            @Override
                            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                            //value change
                            }

                            @Override
                            public void onChildRemoved(DataSnapshot dataSnapshot) {
                                // value remove
                            }

                            @Override
                            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

use addChildEventListener for it. it will one by one value . and if value is add then its method onChildAdded will call.

read full documentation here.

Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50