Java notifyDataSetChanged在片段上工作不正常

Java notifyDataSetChanged在片段上工作不正常,java,android,android-fragments,google-cloud-firestore,notifydatasetchanged,Java,Android,Android Fragments,Google Cloud Firestore,Notifydatasetchanged,如标题所述;我发现自己无法获得所需的片段来正确更新 我有一个向用户显示通知的片段,它应该在每次将文档添加到类/数据库时更新。但是,当我从数据库中手动删除文档时,该类似乎没有更新,它显示了以前在数据库中找到的文档。此外,如果我打开和关闭应用程序,它会显示当前文档 片段: public class NotificationFragment extends android.support.v4.app.Fragment { private RecyclerView mNotificationList

如标题所述;我发现自己无法获得所需的片段来正确更新

我有一个向用户显示通知的片段,它应该在每次将文档添加到类/数据库时更新。但是,当我从数据库中手动删除文档时,该类似乎没有更新,它显示了以前在数据库中找到的文档。此外,如果我打开和关闭应用程序,它会显示当前文档

片段:

public class NotificationFragment extends android.support.v4.app.Fragment {

private RecyclerView mNotificationList;
private NotificationsAdapter notificationsAdapter;
private List<Notifications> mNotifList;
private FirebaseFirestore mFirestore;


public NotificationFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_notification, container, false);


    mNotifList = new ArrayList<>();

    mNotificationList = (RecyclerView) v.findViewById(R.id.notification_list);
    notificationsAdapter = new NotificationsAdapter(getContext(), mNotifList);


    mNotificationList.setHasFixedSize(true);
    mNotificationList.setLayoutManager(new LinearLayoutManager(container.getContext()));
    mNotificationList.setAdapter(notificationsAdapter);

    mFirestore = FirebaseFirestore.getInstance();

    String current_user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();

    mFirestore.collection("Users").document(current_user_id).collection("Notifications").addSnapshotListener(requireActivity(), new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (documentSnapshots != null && !documentSnapshots.isEmpty()) {
                for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                    if (doc.getType() == DocumentChange.Type.ADDED) {
                        Notifications notifications = doc.getDocument().toObject(Notifications.class);
                        mNotifList.add(notifications);
                        notificationsAdapter.notifyDataSetChanged();

                    }
                }

            }
        }
    });


    return v;
}
公共类NotificationFragment扩展了android.support.v4.app.Fragment{
私人回收站查看通知列表;
私人通知助理通知助理;
私有列表mNotifList;
私人FirebaseFirestore mFirestore;
公共通知片段(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图v=充气机。充气(R.layout.fragment_通知,容器,错误);
mNotifList=newarraylist();
mNotificationList=(RecyclerView)v.findViewById(R.id.notification\u list);
notificationsAdapter=newnotificationsadapter(getContext(),mNotifList);
mNotificationList.setHasFixedSize(true);
mNotificationList.setLayoutManager(新的LinearLayoutManager(container.getContext());
mNotificationList.setAdapter(notificationsAdapter);
mFirestore=FirebaseFirestore.getInstance();
字符串current_user_id=FirebaseAuth.getInstance().getCurrentUser().getUid();
mFirestore.collection(“Users”).document(当前用户id).collection(“通知”).addSnapshotListener(requireActivity(),new EventListener()){
@凌驾
public void OneEvent(QuerySnapshot文档快照,FireBaseFireStore异常e){
if(documentSnapshots!=null&&!documentSnapshots.isEmpty()){
对于(DocumentChange文档:documentSnapshots.getDocumentChanges()){
if(doc.getType()==DocumentChange.Type.ADDED){
通知通知=doc.getDocument().toObject(Notifications.class);
mNotifList.add(通知);
notificationsAdapter.notifyDataSetChanged();
}
}
}
}
});
返回v;
}
数据库结构:


如果要删除任何数据集,必须将notifyItemRemoved与数据的位置一起设置

例如:

mDataset.remove(position); // remove your data
notifyItemRemoved(position); // notify that your data is removed
notifyItemRangeChanged(position, mDataSet.size()); // you can use if range is changed 

在NotificationsAdapter.class中使用下面给定的方法,然后调用此方法,而不是在片段中直接调用notifyDataSetChanged()。实际上,您并没有将数据传递给引起问题的适配器

public void updateAdapter(ArrayList<Notifications> mDataList) {
        this.mList = mDataList;
        notifyDataSetChanged();
    }
public void updatedapter(ArrayList mDataList){
this.mList=mDataList;
notifyDataSetChanged();
}

您在RecycleServiceAdapter中引用了吗?可能您需要检查
是否(doc.getType()==DocumentChange.Type.REMOVED)
在您的
OneEvent
中添加了您的数据库结构,并请使用@AlexMamo@AlexMamo我的数据库结构如下:Users/“User id”/Notifications/“通知/“我为每个用户存储此集合中的所有文档。请让我知道这是否是您所指的。谢谢。这没有帮助。请添加一个屏幕截图。@AlexMamo我添加了屏幕截图,但它似乎没有显示出来。我如您所述做了,但每当我在片段中调用该方法时,它都会给我一个错误。下面是ho我在片段和适配器中都有代码。适配器:public void updatedapter(ArrayList mDataList){this.mNotifList=mDataList;notifyDataSetChanged();}片段:notificationsAdapter.updatedapter();方法中缺少参数。像这样使用notificationsAdapter.updatedapter(mNotificationList);