如何在Firebase数据库中的值更改时执行命令?

时间:2017-03-02 23:21:43

标签: android firebase firebase-realtime-database

每次更改Firebase-Database中的数据时如何执行命令?

1 个答案:

答案 0 :(得分:1)

You need a ValueEventListener with the onDataChange event callback as specified here. With that you access the dataSnapshot argument and determine the appropriate action to take. Example from the documentation:

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Post post = dataSnapshot.getValue(Post.class);
        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
};
mPostReference.addValueEventListener(postListener);