简体   繁体   English

为什么在 firestore 中添加新数据时 recyclerview 会刷新?

[英]Why does recyclerview refreshed when new data is added in firestore?

I don't want to refresh recyclerview items when a new data is added in firestore database.在 firestore 数据库中添加新数据时,我不想刷新 recyclerview 项目。 Firestore returns all documents from Student collection every time a new student is added in Firestore database.每次在 Firestore 数据库中添加新学生时,Firestore 都会返回 Student 集合中的所有文档。 How should I prevent firestore from returning all documents when a new student is added?添加新学生时,我应该如何防止 firestore 返回所有文档?

Here is my code:这是我的代码:

private void load() {
    Query query = createQuery();
    FirestoreRecyclerOptions<Student> options = new FirestoreRecyclerOptions.Builder<Student>()
            .setQuery(query, Student.class)
            .build();

    myStudentAdapter = new MyStudentAdapter(options);
    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");
        }
    }
}

@Override
public void onStart() {
    super.onStart();
    myStudentAdapter.startListening();
}

@Override
public void onStop() {
    super.onStop();
    myStudentAdapter.stopListening();
}

The adapters in the FirebaseUI library: they monitor the data on the server, and update to reflect changes in that data. FirebaseUI 库中的适配器:它们监视服务器上的数据,并更新以反映该数据的变化。 There is no way to change the behaviors of these adapters to only load the data, and skip the monitoring.无法将这些适配器的行为更改为仅加载数据,而跳过监视。

If you don't want to reflect changes, you'll have to implement your own adapter.如果您不想反映更改,则必须实现自己的适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我将数据更新到 Firestore 时 RecyclerView 崩溃 - RecyclerView crashes when I update data to Firestore 设置新文档数据时出现 Firestore 错误 - Firestore error when setting new document data 为什么recyclerview数据只有在第二次点击按钮时才加载 - Why does the recyclerview data loads only when the button is clicked the second time 如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据? - How to display data from Firestore in a RecyclerView with Android? Firestore:创建新文档时创建隐藏数据 - Firestore: create hidden data when creating new document 为什么 Firestore 数据库中的数据在 FirestoreRecyclerAdapter 中返回为“null”? - Why does data from Firestore DB returns as 'null' in FirestoreRecyclerAdapter? 如何使用 firebase childEventListener 更新平滑 animation 添加的 Recyclerview 新项目? - How to update Recyclerview new item added with smooth animation with firebase childEventListener? 有没有办法显示 Data Studio 报告中的数据最后一次刷新的时间? - Is there a way to show when the data in a Data Studio report was last refreshed? 从 Firestore 中提取数据时,Flatlist 不呈现 - Flatlist does not render when pulling data from firestore 使用 Flutter 和 Firestore 中的新数据更新 StreamBuilder - Updating a StreamBuilder with new data in Flutter and Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM