Search code examples
javaandroidfirebasefirebaseui

Is it possible to implement FirebaseRecycler in a Fragment?


I am building an app with a similar layout as Venmo where there are three ViewPagers (Home, Friends, Public) that each display data from Firebase in a continuously updating view. My initial idea was to use a RecyclerView from the FirebaseUI library but several questions related to mine say that this is not possible since Firebase does not work well with fragments. I was wondering if this was still the case as the questions were several years old.

I already have the ViewPager set up and working properly and even have a RecyclerView populated with CardViews. However, they are not getting data from Firebase. I was using this SO answer to try and acheive my goal How to display data from Firestore in a RecyclerView with Android? but ran into a problem when implementing the FirebaseRecyclerOptions object. This code is inside my onCreateView method of my fragments:

FirebaseRecyclerOptions<Post> options = new FirebaseRecyclerOptions.Builder<Post>()
                .setQuery(query,Post.class)
                .build();

The error that I am getting is "Cannot resolve method 'setQuery()."

I assume that the issue is Post.class cannot be called in a fragment but I am unsure how else to call it. I may be doing this in the completely wrong way but appreciate any help.


Solution

  • I found the answer. As I was following the linked question above I got to this line of code:

    FirestoreRecyclerOptions<Post> options = new FirestoreRecyclerOptions.Builder<Post>()
                    .setQuery(query,Post.class)
                    .build();
    

    This wasn't working for me and was giving error that it Could not resolve FirestoreRecyclerOptions but I was unable to find out why. Turns out I was missing:

    implementation 'com.google.firebase:firebase-firestore:17.0.4'
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.firebaseui:firebase-ui-firestore:4.1.0'
    

    In my build.Gradle.