0

I'm using the FirebaseRecyclerAdapter to display a list of announcements to students in a university app training project I'm working on. The data is stored in the folowing structure:

each student has a department, year and a language field students reference

each announcement has a targeted department, year and language enter image description here my goal is to only display the announcements that are targetted to the currently logged in student, but I couldn't find a way to perform such query in firebase database.

fetching all the announcements wouldn't cause any privacy issues, so I thought of filtering the data in the FirebaseRecyclerAdapter. I saw (this) answer that implements a Filterable then calls the FirebaseRecyclerAdapter.getFilter().filter() method to do the filtering. However, I need to run the filter() method automatically everytime more data is fetched from the database and I have no idea how to do this.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kamel Fakih
  • 47
  • 1
  • 4

1 Answers1

0

To get only the announcements for the current user, you need to do two steps:

  1. Load the current user, so you can look up their department.
  2. Create a query for only the announcements of that department, something like:

Code:

Query query = FirebaseDatabase.getInstance().getRef("Announcements").orderByChild("department").equalTo("CCNE");

You can then create the FirebaseRecyclerAdapter based on the query, instead of the direct reference to all Announcements.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • My problem is that I can't do such query, because I need to filter based on year and language too, not only the department – Kamel Fakih Oct 04 '21 at 15:06
  • That would've been useful to include in your question. But see http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Oct 04 '21 at 15:23