My goal is to allow users to filter according to category. And I came across this .orderBy().startAt() thing, in my case would be .orderBy("category").startAt(category's string), I'm able to set up the recycler view, but it does not update everytime different category or string is choosen in the Spinner.
Here's my code
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner sortingSpinner = findViewById(R.id.spinner_main_sorting);
String[] sortingCategory = new String[]{"Business", "Life", "Etc"};
ArrayAdapter<String> spinnerAdapter2 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item,
sortingCategory);
sortingSpinner.setAdapter(spinnerAdapter2);
String category_sorted = sortingSpinner.getSelectedItem().toString();
setUpRecyclerView();
}
Setting up RecyclerView method
private void setUpRecyclerView() {
Query query = MainFeedRef.orderBy("category").startAt(category_sorted);
FirestoreRecyclerOptions<Entry> options = new FirestoreRecyclerOptions.Builder<Entry>()
.setQuery(query, Entry.class)
.build();
adapter = new AddEntryAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view_main);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
I guess it's not refreshed cause the set up method only run once as it is onCreate() right..? May someone guide me how to make it so it can refresh for every category choosen.
A Firestore query is immutable: once you've created it, it cannot be modified. So every times the search value changes, you'll need to create a new query, and pass that to the existing adapter by calling its updateOptions
method.
Also see: