Listen to changes in Firebase Database with Android

0

I have some data in Firebase Database:

- order
    -catched : false
    -completed : false
- user
    -name : ejemplo

I would like that when you move to an Activity, it will remain listening in a persistent manner if a change occurs in "catched", in this case if it changes to "true"; in order to be able to execute another action when said event occurs. How could I do this?

Thanks and I remain attentive.

    
asked by Didier 05.05.2017 в 04:59
source

1 answer

1

(I suppose that the project in FB is already created and your app in android with the necessary aggregates)

1) Reference

DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mOrderRef = mRootRef.child("order");

2) Get the value of the desired child

mOrderRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            boolean mCatched = dataSnapshot.child("catched").getValue(boolean.class); 

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

3) The value is now in mCatched and you can use it as necessary.

    
answered by 08.05.2017 / 01:40
source