I am using a FirestoreRecyclerAdapter to display my items in a recyclerView. Using searchView, i am able to create a query:
querySearch = FirebaseFirestore.getInstance()
.collection("App").document(" " + userID).collection("reminders")
.startAt("title", newText)
.endAt("title", newText+"\uf8ff");
My question is, how can I pass the query to FirestoreRecyclerOptions so that my recyclerView displays what I am searching for?
(I'm using Cloud Firestore)
EDIT: here is my updated code
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
querySearch = FirebaseFirestore.getInstance()
.collection("App").document(" " + userID).collection("reminders")
.orderBy(newText);
FirestoreRecyclerOptions<Reminder> options = new FirestoreRecyclerOptions.Builder<Reminder>()
.setQuery(querySearch, Reminder.class)
.build();
//create new FirestoreRecyclerAdapter:
adapterSearch = new FirestoreRecyclerAdapter<Reminder, ReminderHolder>(options) {
@Override
public void onBindViewHolder(final ReminderHolder holder, int position, final Reminder model) {
holder.bind(model);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openEditPopup();
DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition());
docID = snapshot.getId();
}
});
}
@Override
public ReminderHolder onCreateViewHolder(ViewGroup group, int i) {
View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_beta, group, false); //todo use beta item layout
return new ReminderHolder(view);
}
};
recyclerView.setAdapter(adapterSearch);
adapterSearch.startListening(); //connects to firebase collection
adapterSearch.notifyDataSetChanged();
You need to use FirebaseUI for Cloud Firestore to be able to use the class FirestoreRecyclerOptions
, add the following dependencies in the build.gradle
file:
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'
then you can do following:
querySearch = FirebaseFirestore.getInstance()
.collection("App").document(" " + userID).collection("reminders")
.startAt("title", newText)
.endAt("title", newText+"\uf8ff");
FirestoreRecyclerOptions<ModelClass.class> options = new FirestoreRecyclerOptions.Builder<ModelClass.class>()
.setQuery(querySearch, ModelClass.class)
.build();
Also check the docs.