Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用addSnapshotListener时如何查看Firestore的实时更改?_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Android 使用addSnapshotListener时如何查看Firestore的实时更改?

Android 使用addSnapshotListener时如何查看Firestore的实时更改?,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我有一个产品的集合,我想得到实时更新。这是我的代码: query.addSnapshotListener(new EventListener<QuerySnapshot>() { @Override public void onEvent(@Nullable QuerySnapshot querySnapshot, @Nullable FirebaseFirestoreException e) { if (e != null) return;

我有一个产品的集合,我想得到实时更新。这是我的代码:

query.addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot querySnapshot, @Nullable FirebaseFirestoreException e) {
        if (e != null) return;

        List<Product> list = new ArrayList<>();
        for (DocumentChange documentChange : querySnapshot.getDocumentChanges()) {
            switch (documentChange.getType()) {
                case ADDED:
                    Product product = documentChange.getDocument().toObject(Product.class);
                    list.add(product);
                    break;
                case MODIFIED:
                    adapter.notifyDataSetChanged();
                    break;
                case REMOVED:
                    //
                    break;
            }
        }
        adapter = new ProductAdapter(context, list);
        recyclerView.setAdapter(adapter);
    }
});

同样的事情也会发生。仍然看到旧的价格。

您还没有将更新的产品数据放入
列表中

case MODIFIED:
    Product product = documentChange.getDocument().toObject(Product.class);
    // TODO: Replace the existing product in the list with the updated one
    adapter.notifyDataSetChanged();

谢谢你的回答,但它不起作用。请看我编辑的问题,“它不起作用”不是很有帮助。对于该代码,有什么特别不起作用?你是不是在调试器中单步调试的?它是否找到产品并将其移除?您添加的新产品(
list.add(modifiedProduct)
)是否具有新属性(在调试器中选中时)?是的,您是对的,我调试了代码,现在它可以工作了,这要感谢您。非常感谢。你是否碰巧知道,当价格发生变化时,为什么要滚动到第一个位置?我如何避免这种情况?我不知道。但我敢打赌,搜索应该会给出一些相关的答案。例如,这看起来很有希望:
case MODIFIED:
    Product product = documentChange.getDocument().toObject(Product.class);
    // TODO: Replace the existing product in the list with the updated one
    adapter.notifyDataSetChanged();