Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step 4:Error: (58, 56) - in the line mRegistration = mQuery.addSnapshotListener(this); #12

Open
jadder opened this issue Mar 18, 2018 · 6 comments
Assignees

Comments

@jadder
Copy link

jadder commented Mar 18, 2018

mRegistration = mQuery.addSnapshotListener(this);

"this" is getting me this error.

Error:(58, 56) error: incompatible types: FirestoreAdapter<VH> cannot be converted to EventListener<QuerySnapshot> where VH is a type-variable: VH extends ViewHolder declared in class FirestoreAdapter

NOTE: the interface EventLister is empty , so I just have to comment the
//@OverRide
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

Code bellow

/**
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package com.google.firebase.example.fireeats.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.ListenerRegistration;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.EventListener;

/**
 * RecyclerView adapter for displaying the results of a Firestore {@link Query}.
 *
 * Note that this class forgoes some efficiency to gain simplicity. For example, the result of
 * {@link DocumentSnapshot#toObject(Class)} is not cached so the same object may be deserialized
 * many times as the user scrolls.
 * 
 * See the adapter classes in FirebaseUI (https://github.com/firebase/FirebaseUI-Android/tree/master/firestore) for a
 * more efficient implementation of a Firestore RecyclerView Adapter.
 */
public abstract class FirestoreAdapter<VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> implements EventListener {

    private static final String TAG = "Firestore Adapter";

    private Query mQuery;
    private ListenerRegistration mRegistration;

    private ArrayList<DocumentSnapshot> mSnapshots = new ArrayList<>();

    public FirestoreAdapter(Query query) {
        mQuery = query;
    }

    public void startListening() {
        // TODO(developer): Implement
        if (mQuery != null && mRegistration == null) {
            mRegistration = mQuery.addSnapshotListener(this);
        }
    }

    public void stopListening() {
        if (mRegistration != null) {
            mRegistration.remove();
            mRegistration = null;
        }

        mSnapshots.clear();
        notifyDataSetChanged();
    }


    public void setQuery(Query query) {
        // Stop listening
        stopListening();

        // Clear existinkodig data
        mSnapshots.clear();
        notifyDataSetChanged();

        // Listen to new query
        mQuery = query;
        startListening();
    }


    //@Override
    public void onEvent(QuerySnapshot documentSnapshots,  FirebaseFirestoreException e) {

        // Handle errors
        if (e != null) {
            Log.w(TAG, "onEvent:error", e);
            return;
        }

        // Dispatch the event
        for (DocumentChange change : documentSnapshots.getDocumentChanges()) {
            // Snapshot of the changed document
            DocumentSnapshot snapshot = change.getDocument();

            switch (change.getType()) {
                case ADDED:
                    // TODO: handle document added
                    onDocumentAdded(change);
                    break;
                case MODIFIED:
                    // TODO: handle document modified
                    onDocumentModified(change);
                    break;
                case REMOVED:
                    // TODO: handle document removed
                    onDocumentRemoved(change);
                    break;
            }
        }
        onDataChanged();
    }


    protected void onDocumentAdded(DocumentChange change){
        mSnapshots.add(change.getNewIndex(), change.getDocument());
        notifyItemInserted(change.getNewIndex());
    }

    protected void onDocumentModified(DocumentChange change){
        if (change.getOldIndex() == change.getNewIndex()) {
            mSnapshots.set(change.getOldIndex(), change.getDocument());
            notifyItemChanged(change.getOldIndex());
        } else {
            mSnapshots.remove(change.getOldIndex());
            mSnapshots.add(change.getNewIndex(), change.getDocument());
            notifyItemMoved(change.getOldIndex(), change.getNewIndex());
        }
    }

    protected void onDocumentRemoved(DocumentChange change){
        mSnapshots.remove(change.getOldIndex());
        notifyItemRemoved(change.getOldIndex());
    }

    @Override
    public int getItemCount() {
        return mSnapshots.size();
    }

    protected DocumentSnapshot getSnapshot(int index) {
        return mSnapshots.get(index);
    }

    protected void onError(FirebaseFirestoreException e) {};

    protected void onDataChanged() {}
}
@samtstern
Copy link
Contributor

@jadder you imported java.util.EventListener but you meant to import com.google.firebase.firestore.EventListener

@joeedwin
Copy link

joeedwin commented Aug 5, 2018

Error:(60, 56) error: incompatible types: FirestoreAdapter cannot be converted to EventListener
where VH is a type-variable:
VH extends ViewHolder declared in class FirestoreAdapter

it gives this error even though i imported the statement you said

@0xBABA
Copy link

0xBABA commented Mar 28, 2019

This issue still exists:
addSnapshotListener(com.google.firebase.firestore.EventListener<com.google.firebase.firestore.QuerySnapshot>) in Query cannot be applied to (com.google.firebase.example.fireeats.adapter.FirestoreAdapter)

I am also not sure how com.google.firebase.firestore.EventListener is related since addSnapshotListener has the following variants:

image

@samtstern
Copy link
Contributor

@kroikie something for you to look at when you're tackling codelabs bugs.

@Apsakash1
Copy link

@jadder you imported java.util.EventListener but you meant to import com.google.firebase.firestore.EventListener

This issue is still there. Even after importing com.google.firebase.firestore.EventListener. Also, @OverRide gives error on onEvent method.
The app runs after commenting @OverRide and typecasting 'this' to '(EventListener) this' but it keeps stopping. Only sign in screen works but the main activity keeps crashing.

@sanjeetjh
Copy link

Still same issue come

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants