firebase / friendlyeats-android

Cloud Firestore Android codelab
https://codelabs.developers.google.com/codelabs/firestore-android
Apache License 2.0
262 stars 156 forks source link

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

Open jadder opened 6 years ago

jadder commented 6 years ago

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 commented 6 years ago

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

joeedwin commented 6 years ago

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 commented 5 years ago

This issue still exists: addSnapshotListener(com.google.firebase.firestore.EventListener) 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 commented 5 years ago

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

Apsakash1 commented 3 years ago

@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 commented 9 months ago

Still same issue come