5

I can check if any data that is a child of my reference has changed no problem but I cant find a way to check if a certain field has changed within that child.

Here is some code the .child("job") does not work as job is the field. It was just somethin I was trying.

 Firebase ref = new Firebase(Config.FIREBASE_URL);

        //adding a value event listener so if data in database changes it does in textview also not needed at the minute
        ref.child("Driver").child("Driver2").child("job").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {

                for (DataSnapshot postSnapshot : snapshot.getChildren()) {


                    Toast.makeText(ProfileActivity.this, "YEAAA ", Toast.LENGTH_SHORT).show();

                }

            }

            /************had to implement this method****************/
            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());

            }
        });

here is the json from the database

  "Driver" : {
    "Driver1" : {
      "job" : "none",
      "lat" : 53.4013419,
      "lon" : -111.803055,
      "name" : "JOHNNY"
    },
    "Driver2" : {
      "job" : "job",
      "lat" : 53.4012956,
      "lon" : -6.4091191,
      "name" : "MICK"
    },
    "Driver3" : {
      "job" : "cccc",
      "lat" : 45.815399,
      "lon" : 15.966568,
      "name" : "Boris"
    }
  }

I want to check if the job field changes, as the coordinates fields will be constantly changing anyway.

AL.
  • 36,815
  • 10
  • 142
  • 281
AnonymousAlias
  • 1,149
  • 2
  • 27
  • 68

1 Answers1

7

You are using correctly addValueEventListener() on the reference but you don't need that for loop. Your DataSnapshot is already on the job child, in which the key is job and the value is: none. So you need to remove the iteration. Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • So you are saying I can take away the for loop, but then what code can I use to see if the value of job changes from none to something else? and not if anything under driver2 changes because the lat and lon are constantly changing – AnonymousAlias Apr 06 '17 at 21:28
  • So if you want to display data from a Firebase database that is continually changing, i recomand you to use `FirebaseListAdapter`. Every change that is made is update automatically in a `ListView` or a `RecyclerView`. – Alex Mamo Apr 06 '17 at 21:32
  • I have no problem with the data that is changing, the only issue I have is that I was to trigger an event based on whether the job field changes. – AnonymousAlias Apr 06 '17 at 21:33
  • So i specifically want to check if the job field changes rather than if any fields in Driver2 change – AnonymousAlias Apr 06 '17 at 21:35
  • I understand, so this is how you need to do. You need put the `addValueEventListener()` on the correct child, as mentioned above and you will see only the changes on that particular field. – Alex Mamo Apr 06 '17 at 21:37
  • Thats the problem I have though, If if put it on the child Driver2 it checks all fields in Driver2, and it doesnt recognise .child("job") in the event listener – AnonymousAlias Apr 06 '17 at 21:40
  • Use a `Toast` like this: `Toast.makeText(ProfileActivity.this, snapshot.getValue().toString, Toast.LENGTH_SHORT).show();` – Alex Mamo Apr 06 '17 at 21:42
  • Glad to help! Cheers! – Alex Mamo Apr 06 '17 at 21:49