ChildEventListener должен реализовать onChildChanged

Это сводит меня с ума. Я использую «onChildAdded», и если я переключу его на «onChildChanged», то он попросит меня вернуться к «onChildAdded». Я понятия не имею, почему он это делает.

Вот мой код:

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

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

        });

И полная ошибка:

Class 'Anonymous class derived from ChildEventListener' must either be declared abstract or implement abstract method 'onChildChanged(DataSnapshot, String)' in 'ChildEventListener'

0
0
217
2
Перейти к ответу Данный вопрос помечен как решенный

Ответы 2

Возможно, вы захотите добавить аннотацию @Override к реализованному методу .onChildChanged(), иначе он не будет распознан как реализация метода abstract. документация гласит:

Indicates that a method declaration is intended to override a method declaration in a supertype.

Ответ принят как подходящий

Если вы хотите реализовать ChildEventListener, вы должны переопределить 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);

Другие вопросы по теме