-1

I'm having issues with the below code

private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseReference;
private ChildEventListener mChildListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mDatabaseReference = mFirebaseDatabase.getReference().child("MyItem");
    mChildListener = new ChildEventListener(); 
    mDatabaseReference.addChildEventListener(mChildListener);
}

The error is childeventlistener is abstract cannot be instantised for the below line mChildListner = new ChildEventListener()

Any idea what i'm doing wrong.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Newbie
  • 1
  • 1
  • 1
    the error is very clear , you can't create the object of an abstract class, do you know what an abstract class is? – Abhinav Chauhan Apr 21 '20 at 10:54
  • Sorry but i'm a newbie in programming(still no excuse) but i did read up on it before posting on here and was still confused with the error. I found that an abstract class is a method that is declared and may require subclasses. – Newbie Apr 21 '20 at 11:04
  • video about abstract class https://youtu.be/3sDxmJVQhA0 don't mind his accent it is a very good explanation and here are many answers about abstract class https://stackoverflow.com/questions/1320745/abstract-class-in-java in encourage to to understand java basics first before starting with firebase – Abhinav Chauhan Apr 21 '20 at 11:12
  • Thanks. Will do that – Newbie Apr 21 '20 at 11:20
  • although the `ChildEventListner` is not an abstract class it is an interface , but you will get the idea – Abhinav Chauhan Apr 21 '20 at 11:23

1 Answers1

1

ChildEventListener is an interface you can't in java create an instance of that, instead you need to make a class that implements this interface.

instead of this:

mChildListener = new ChildEventListener(); 
mDatabaseReference.addChildEventListener(mChildListener);

You do this:

........

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


    }

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


    }

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


    }

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


    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "postComments:onCancelled", databaseError.toException());

    }
};


mDatabaseReference.addChildEventListener(mChildListener);
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22