简体   繁体   English

notifyDataSetChanged在片段上无法正常工作

[英]notifyDataSetChanged not working properly on fragment

As described in the title; 如标题中所述; I find myself unable to get the desired fragment to update properly. 我发现自己无法获得所需的片段以正确更新。

I have a fragment that shows notifications to the user, and it should be updated every time a document gets added to the class/database. 我有一个片段,向用户显示通知,并且每次将文档添加到类/数据库时都应该对其进行更新。 However, when I delete the document manually from the database, the class does not seem to be updated and it shows the documents previously found in the database. 但是,当我从数据库中手动删除文档时,该类似乎没有更新,并且显示了以前在数据库中找到的文档。 Additionally, it does, however, show the current documents if I open and close the application. 另外,如果我打开和关闭应用程序,它还会显示当前文档。

Fragment: 分段:

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;
}

Database Structure: 数据库结构:

数据库结构

Use the below given method inside your NotificationsAdapter.class and then call this method instead of calling notifyDataSetChanged() directly in your Fragment. 在NotificationsAdapter.class中使用下面给定的方法,然后调用此方法,而不是直接在Fragment中调用notifyDataSetChanged()。 Actually you are not passing data to the Adapter that was the issue. 实际上,您没有将数据传递给问题的适配器。

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

If you want to remove any data set you have to set notifyItemRemoved with position of the data. 如果要删除任何数据集,则必须设置带数据位置的notifyItemRemoved。

Example: 例:

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 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM