YQadoome
YQadoome

Reputation: 178

Load Geofirestore documents results into RecyclerView

I use GeoFirestore to find the nearest location, it worked but I need to load the documents that I got into RecyclerView, how can possibly achieve that?

GeoFirestore method

   private void getNearestEstate(){

    GeoQuery geoQuery = geoFirestore.queryAtLocation(new GeoPoint(latitude, longitude), 2);
    geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
        @Override
        public void onDocumentEntered(DocumentSnapshot documentSnapshot, GeoPoint geoPoint) {

        }
}

load into Rv

   private void dataFetch(Query query,int rvView){


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

        Toast.makeText(getContext(),options.toString(),Toast.LENGTH_LONG).show();
    mAdapter = new DiscoverAdapter(options);
    RecyclerView recyclerView = root.findViewById(rvView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
    recyclerView.setAdapter(mAdapter);
    mAdapter.startListening();
    }

Upvotes: 0

Views: 286

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

When you perform a GeoQuery against a Cloud Firestore database and use addGeoQueryDataEventListener(), there are a few methods that must be implemented. As in your example, one of those methods is called onDocumentEntered(). As you can see, it has two arguments, the first one being of type DocumentSnapshot. This object contains all the locations objects in your range. In order to be able to display those locations, you need to iterate through the DocumentSnapshot object and get all locations.

To display all the locations in a RecyclerView, you first need to create a list of locations. So when you iterate, add all location objects to a list. Then simply create an adapter and pass that list to it's constructor. Set the adapter to the RecyclerView and you are done.

If you thought you can pass the GeoQuery to a FirestoreRecyclerOptions object, as you normally do when querying a Cloud Firestore database, please note that this is not possible, since GeoQuery class does not extend com.google.firebase.firestore.Query.

Upvotes: 2

Related Questions