Android ChildEventListener必须实现onChildChanged

Android ChildEventListener必须实现onChildChanged,android,firebase-realtime-database,Android,Firebase Realtime Database,这快把我逼疯了。我使用的是“onchildaded”,如果我将其切换为“onChildChanged”,那么它会要求我切换回“onchildaded”。我不知道它为什么这样做 这是我的密码: Query queryRecycler = mDatabase.limitToLast(5); queryRecycler.addChildEventListener(new ChildEventListener() { public void onChildAdd

这快把我逼疯了。我使用的是“onchildaded”,如果我将其切换为“onChildChanged”,那么它会要求我切换回“onchildaded”。我不知道它为什么这样做

这是我的密码:

Query queryRecycler = mDatabase.limitToLast(5);
        queryRecycler.addChildEventListener(new ChildEventListener() {

            public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
                messageList.add(dataSnapshot.getValue(Message.class));
                mMessageAdapter.notifyDataSetChanged();
            }

        });
完全错误是:

类“从ChildEventListener派生的匿名类”必须为 声明抽象或实现抽象方法 “ChildEventListener”中的“onChildChanged(DataSnapshot,String)”


您可能希望将
@Override
注释添加到实现的方法
.onChildChanged()
,否则它将不会被识别为
抽象方法的实现。草案内容如下:

指示方法声明旨在重写超类型中的方法声明


您可能希望将
@Override
注释添加到实现的方法
.onChildChanged()
,否则它将不会被识别为
抽象方法的实现。草案内容如下:

指示方法声明旨在重写超类型中的方法声明

如果要实现,应该重写onChildAdded、onChildChanged、onChildRemoved和onChildMoved。即使你不想要它。(源代码示例)

如果要实现,应该重写onChildAdded、onChildChanged、onChildRemoved和onChildMoved。即使你不想要它。(源代码示例)

ChildEventListener childEventListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());

        // A new comment has been added, add it to the displayed list
        Comment comment = dataSnapshot.getValue(Comment.class);

        // ...
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());

        // A comment has changed, use the key to determine if we are displaying this
        // comment and if so displayed the changed comment.
        Comment newComment = dataSnapshot.getValue(Comment.class);
        String commentKey = dataSnapshot.getKey();

        // ...
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
        Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());

        // A comment has changed, use the key to determine if we are displaying this
        // comment and if so remove it.
        String commentKey = dataSnapshot.getKey();

        // ...
    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());

        // A comment has changed position, use the key to determine if we are
        // displaying this comment and if so move it.
        Comment movedComment = dataSnapshot.getValue(Comment.class);
        String commentKey = dataSnapshot.getKey();

        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "postComments:onCancelled", databaseError.toException());
        Toast.makeText(mContext, "Failed to load comments.",
                Toast.LENGTH_SHORT).show();
    }
};
ref.addChildEventListener(childEventListener);