2

Database Screenshot I currently have a database that is set up like below,

Raters- |_userID1 | |_Car1 | |_Car2 | |_Car3 |_userID2 |_Car1 |_Car2 |_Car3

With Raters being the base collection, the userIDs being the documents, and the Cars being a subCollection of custom Objects. I am trying to retrieve the subcollection for the current user(based on the emailaddress) and populate a recyclerView with it using Firestore UI RecyclerViewAdapter. I understand that I cannot query a subcollection but is there any way I can retrieve the CollectionReference and populate my RecyclerView with that? I need to do it this way because I am trying to implement a "swipe to rate" function and I cannot remove the car object from the RecyclerView without deleting it from the database (which is why I do not have a Car collection) so I plan on removing it from the users "nonRatedCars" subcollection and adding it to a new "ratedCars" subcollection.

 Query query = ratersReference.document(usernameIn).collection("nonRatedCars");

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


    adapter = new CarAdapter(options);
    RecyclerView recyclerView = findViewById(R.id.recycler_view);
    llm = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(llm);
    recyclerView.setAdapter(adapter);
Garry645
  • 23
  • 5
  • 1
    *"I understand that I cannot query a subcollection"*. You most certainly can query a subcollection, just like any other top-level collection. – Doug Stevenson Mar 20 '19 at 21:05
  • I have tried to query using "Query query = ratersReference.document(usernameIn).collection("nonRatedCars");" but the recyclerView just shows up empty when I load the app – Garry645 Mar 20 '19 at 21:08
  • That doesn't mean you can't query a subcollection. That just means your query returned nothing, or you didn't wire up your RecyclerView properly. Without showing the code of what you're trying to do, and the data in your database, we have no idea what went wrong. – Doug Stevenson Mar 20 '19 at 21:09
  • I have included the code for my recyclerview setup and a screenshot of my database. I can also include the adapter if you would like but it is just a basic FirestoreRecyclerViewAdapter – Garry645 Mar 20 '19 at 21:21
  • What are the values for `ratersReference` and `usernameIn`? – Alex Mamo Mar 21 '19 at 07:45
  • Nevermind I forgot to set the adapter to startListening() in the onStart() method. Thank you for the help though – Garry645 Mar 21 '19 at 16:13

0 Answers0