Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.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 如何使用快照侦听器在recyclerview中获取实时更新?_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Android 如何使用快照侦听器在recyclerview中获取实时更新?

Android 如何使用快照侦听器在recyclerview中获取实时更新?,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,下面是我用来在recyclerview中检索文档数据的代码。它很好用。但无论何时添加新文档,它都不会实时更新。我知道snapshot listener仅用于此目的,但很难让它正常工作。任何帮助都将不胜感激。:) mFirestore.collection(“Users”).get().addOnCompleteListener(新的OnCompleteListener()){ @凌驾 未完成的公共void(@NonNull任务){ if(task.issusccessful()){ 对于(Doc

下面是我用来在recyclerview中检索文档数据的代码。它很好用。但无论何时添加新文档,它都不会实时更新。我知道snapshot listener仅用于此目的,但很难让它正常工作。任何帮助都将不胜感激。:)

mFirestore.collection(“Users”).get().addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(DocumentSnapshot文档:task.getResult()){
Message Message=document.toObject(Message.class);
messageList.add(消息);
mAdapter.notifyDataSetChanged();
}
}
}
});

如果您想继续监听Firestore数据(实时更新),您应该这样做:

mFirestore.collection(“用户”)
.addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
@可为空的FireBaseFireStore异常(e){
如果(e!=null){
Log.w(“YourTag”,“Listen failed.”,e);
返回;
}
用于(QueryDocumentSnapshot文档:queryDocumentSnapshots){
如果(doc.exists()){
Message Message=doc.toObject(Message.class);
messageList.add(消息);
mAdapter.notifyDataSetChanged();
}
}
Log.d(“YourTag”,“messageList:+messageList”);
}
});
您使用的方法仅检索Firestore数据一次


选中此选项>>

您应该像这样分离快照事件。。然后你就可以很容易地发现,问题出在哪里

mFirestore.collection(“用户”)
.addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable QuerySnapshot快照,
@可为空的FireBaseFireStore异常(e){
如果(e!=null){
Log.w(“标记”,“侦听:错误”,e);
返回;
}
对于(DocumentChange dc:snapshots.getDocumentChanges()){
开关(dc.getType()){
案件补充:
Log.d(“TAG”,“New Msg:”+dc.getDocument().toObject(Message.class));
打破
案例修改:
Log.d(“TAG”,“Modified Msg:”+dc.getDocument().toObject(Message.class));
打破
已删除的案例:
Log.d(“TAG”,“Removed Msg:”+dc.getDocument().toObject(Message.class));
打破
}
}
}
});

可能您得到的快照是由[MODIFIED]事件触发的,而不是[Add]。

请查看如何在recycler视图中使用它:

// Get Chats List From FireStore
private void getChatListFromFireBase(String hiddenStatus) {

    lIndividualChatList = new ArrayList<>();
    lIndividualChatListIds = new ArrayList<>();

    lIndividualChatList.clear();
    lIndividualChatListIds.clear();

    fbFs.collection("individual_chats")
        .document(mySqlUserId)
        .collection("lists")
        .addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots,
                                FirebaseFirestoreException e) {

                if (e != null) {

                }

                for (DocumentChange dc : documentSnapshots.getDocumentChanges()) {
                    switch (dc.getType()) {
                        case ADDED:

                            key = dc.getDocument()
                                    .getId();

                            firebaseRetrofitQuery(dc,
                                                  "Added",
                                                  key);

                            break;
                        case MODIFIED:

                            key = dc.getDocument()
                                    .getId();

                            // Do The Change Function
                            firebaseRetrofitQuery(dc,
                                                  "Changed",
                                                  key);

                            break;
                        case REMOVED:

                            break;
                    }
                }

            }
        });
}



// FireBase And Retrofit Query
private void firebaseRetrofitQuery(DocumentChange documentChange,
                                   final String childActivityType,
                                   final String key) {

    if (childActivityType.equals("Added")) {

        lIndividualChatListIds.add(key);

    }

    // Get The Receiver Id To Get Data From Other Nodes
    final String mySqlFriendUserId = documentChange.getDocument()
                                                   .getId();

    final String hideStatus = (String) documentChange.getDocument()
                                                     .getData()
                                                     .get("hide_status");

    if (childActivityType.equals("Added")) {

        // Populate The Array List With Data From Both Nodes
        lIndividualChatList.add(new IndividualListModel(mySqlFriendUserId,
                                                        hideStatus));

    }

    if (childActivityType.equals("Changed")) {

        int index = lIndividualChatListIds.indexOf(key);
        lIndividualChatList.set(index,
                                new IndividualListModel(mySqlFriendUserId,
                                                        hideStatus));

    }

    // Use The Adapter To Populate The Recycler View
    aIndividualChatList = new IndividualListAdapter(getContext(),
                                                    lIndividualChatList,
                                                    senderActivity,
                                                    new IndividualListAdapter.OnItemClickListener() {

                                                        @Override
                                                        public void onItemClicked(final Map data) {

                                                        }
                                                    });
    rvList.setAdapter(aIndividualChatList);

    aIndividualChatList.notifyDataSetChanged();

}
//从FireStore获取聊天列表
私有void getChatListFromFireBase(字符串hiddenStatus){
lIndividualChatList=newarraylist();
lIndividualChatListIds=newarraylist();
lIndividualChatList.clear();
lIndividualChatListIds.clear();
fbFs.集合(“个人聊天”)
.document(mySqlUserId)
.收集(“清单”)
.addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(QuerySnapshot文档快照,
FireBaseFireStore异常(e){
如果(e!=null){
}
对于(DocumentChange dc:documentSnapshots.getDocumentChanges()){
开关(dc.getType()){
案件补充:
key=dc.getDocument()
.getId();
FirebaseQuery(dc,
“添加”,
键);
打破
案例修改:
key=dc.getDocument()
.getId();
//是否执行更改功能
FirebaseQuery(dc,
“更改”,
键);
打破
已删除的案例:
打破
}
}
}
});
}
//FireBase和改装查询
私有void FirebaseQuery(DocumentChange DocumentChange,
最后一个字符串childActivityType,
最终字符串键){
if(childActivityType.equals(“添加”)){
添加(键);
}
//获取接收器Id以从其他节点获取数据
最后一个字符串mySqlFriendUserId=documentChange.getDocument()
.getId();
最终字符串hideStatus=(字符串)documentChange.getDocument()
.getData()
.获取(“隐藏状态”);
if(childActivityType.equals(“添加”)){
//用两个节点的数据填充数组列表
添加(新的IndividualListModel(mySqlFriendUserId,
希德斯塔斯);
}
if(childActivityType.equals(“已更改”)){
int index=lIndividualChatListIds.indexOf(键);
// Get Chats List From FireStore
private void getChatListFromFireBase(String hiddenStatus) {

    lIndividualChatList = new ArrayList<>();
    lIndividualChatListIds = new ArrayList<>();

    lIndividualChatList.clear();
    lIndividualChatListIds.clear();

    fbFs.collection("individual_chats")
        .document(mySqlUserId)
        .collection("lists")
        .addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots,
                                FirebaseFirestoreException e) {

                if (e != null) {

                }

                for (DocumentChange dc : documentSnapshots.getDocumentChanges()) {
                    switch (dc.getType()) {
                        case ADDED:

                            key = dc.getDocument()
                                    .getId();

                            firebaseRetrofitQuery(dc,
                                                  "Added",
                                                  key);

                            break;
                        case MODIFIED:

                            key = dc.getDocument()
                                    .getId();

                            // Do The Change Function
                            firebaseRetrofitQuery(dc,
                                                  "Changed",
                                                  key);

                            break;
                        case REMOVED:

                            break;
                    }
                }

            }
        });
}



// FireBase And Retrofit Query
private void firebaseRetrofitQuery(DocumentChange documentChange,
                                   final String childActivityType,
                                   final String key) {

    if (childActivityType.equals("Added")) {

        lIndividualChatListIds.add(key);

    }

    // Get The Receiver Id To Get Data From Other Nodes
    final String mySqlFriendUserId = documentChange.getDocument()
                                                   .getId();

    final String hideStatus = (String) documentChange.getDocument()
                                                     .getData()
                                                     .get("hide_status");

    if (childActivityType.equals("Added")) {

        // Populate The Array List With Data From Both Nodes
        lIndividualChatList.add(new IndividualListModel(mySqlFriendUserId,
                                                        hideStatus));

    }

    if (childActivityType.equals("Changed")) {

        int index = lIndividualChatListIds.indexOf(key);
        lIndividualChatList.set(index,
                                new IndividualListModel(mySqlFriendUserId,
                                                        hideStatus));

    }

    // Use The Adapter To Populate The Recycler View
    aIndividualChatList = new IndividualListAdapter(getContext(),
                                                    lIndividualChatList,
                                                    senderActivity,
                                                    new IndividualListAdapter.OnItemClickListener() {

                                                        @Override
                                                        public void onItemClicked(final Map data) {

                                                        }
                                                    });
    rvList.setAdapter(aIndividualChatList);

    aIndividualChatList.notifyDataSetChanged();

}