user1406716
user1406716

Reputation: 9705

Error in reading from Firebase Error: ValueEventListener

I am trying to read from a firebase database using code straight from their documentation. I have my code below and I see the following error when I try to add the ValueEventListener to the my Firebase reference variable.

addValueEventListener (com.firebase.client.ValueEventListener) in Query cannot be applied to (com.firebase.client.ValueEventListener)

I am confused why this error occurs because following is the import statement I use at the top of my Java class file.

import com.google.firebase.database.ValueEventListener;

Basically, I am just trying to get basic read from Firebase running. What am I doing wrong?

public void readfromFireDB() {
    Firebase ref = new Firebase("https://my_project.firebaseio.com/");

    ValueEventListener postListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Question qn = postSnapshot.getValue(Question.class);
                mLog.printToLog("RECEIVED DATA = " + qn.getAnswer() + "," + qn.getLevel());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            mLog.printToLog("in onCancelled, database error = " + databaseError.toString());
        }
    };

    ref.addValueEventListener(postListener); //THIS IS WHERE THE ERROR IS

}

enter image description here

UPDATE: Still having the same issue eve after using the entire classname: com.google.firebase.database.ValueEventListener postListener = new com.google.firebase.database.ValueEventListener() { ... }

enter image description here

Upvotes: 0

Views: 2833

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38299

You're mixing classes from the legacy SDK (2.X) with the new SDK (9.X). Firebase is legacy, ValueEventListener is new.

To use the new SDK, all of your imports should begin with com.google.firebase.database.

Legacy imports begin with com.firebase.client.

I'm assuming you want to use the new SDK. If so, your module build.gradle dependencies should include:

compile 'com.google.firebase:firebase-database:9.4.0'

and should not include:

compile 'com.firebase:firebase-client-android:2.5.0'

Upvotes: 3

Related Questions