Prevent recyclerview from refreshing items when an item is added
I want to display the total results (total item) in a RecyclerView. The items of the RecyclerView may vary based on the filter selected by the user. The problem is that when I changed something in the Firestore database, the total item counts are not based on the filter results anymore, instead, Firestore returns the total items in the entire collection.
private void load() {
Query query = createQuery();
FirestoreRecyclerOptions<Student> options = new FirestoreRecyclerOptions.Builder<Student>()
.setQuery(query, Student.class)
.build();
myStudentAdapter = new MyStudentAdapter(options) {
@Override
public void onDataChanged() {
super.onDataChanged();
tv_total.setText(String.valueOf(getItemCount()));
}
};
rv.setAdapter(myStudentAdapter);
myStudentAdapter.startListening();
}
private Query createQuery() {
if (!search.isEmpty()) {
if (!course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("year", year)
.whereEqualTo("block", block)
.startAt(search)
.endAt(search + '\uf8ff');
} else if (!course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("year", year)
.startAt(search)
.endAt(search + '\uf8ff');
} else if (!course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("block", block)
.startAt(search)
.endAt(search + '\uf8ff');
} else if (course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("year", year)
.whereEqualTo("block", block)
.startAt(search)
.endAt(search + '\uf8ff');
} else if (!course.isEmpty() && year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.startAt(search)
.endAt(search + '\uf8ff');
} else if (course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("year", year)
.startAt(search)
.endAt(search + '\uf8ff');
} else if (course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("block", block)
.startAt(search)
.endAt(search + '\uf8ff');
} else {
return db.collection("Student")
.orderBy("fullName")
开发者_如何转开发 .startAt(search)
.endAt(search + '\uf8ff');
}
} else {
if (!course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("year", year)
.whereEqualTo("block", block);
} else if (!course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("year", year);
} else if (!course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("block", block);
} else if (course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("year", year)
.whereEqualTo("block", block);
} else if (!course.isEmpty() && year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course);
} else if (course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("year", year);
} else if (course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("block", block);
} else {
return db.collection("Student")
.orderBy("fullName");
}
}
}
I want to display the total results (total item) in a RecyclerView.
If you want to know the total number of documents a query returns, then you have to call count() on a Query object, which:
Returns a query that counts the documents in the result set of this query.
If you need that count inside your adapter class, then you have to override getItemCount() that exists inside the FirestoreRecyclerAdapter
class which:
return the total count of snapshots in the adapter.
If you need to refresh the results once you pass another query to your options
object, then you have to call updateOptions(@NonNull FirestoreRecyclerOptions options) in order to take effect.
精彩评论