Android ViewModel 在观察时删除了我的所有数据

问题描述 投票:0回答:1

我正在使用 Android Studio 开发一个应用程序,我希望将 MutableLiveData 与 RecyclerView 一起使用。问题是,当我向 MutableLiveData 添加一个新项目时,它会更新,然后返回到前一个片段,该项目由于某种未知原因被删除。 在我的 FirstFragment.java 代码中为方法 onCreateView:

@Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        binding = FragmentFirstBinding.inflate(inflater, container, false);

        notificationsViewModel = new ViewModelProvider(requireActivity()).get(NotificationsViewModel.class);

        // Binding recycler views
        // Notifications

        RecyclerView rvNotification = binding.registeredNotification;

        Log.d("NA", "New Adapter");
        NotificationAdapter adapter = new NotificationAdapter();

        Log.d("NA", "Setting adapter");
        rvNotification.setAdapter(adapter);

        Log.d("NA", "Setting layout");
        rvNotification.setLayoutManager(new LinearLayoutManager(getActivity()));

        Log.d("NA", "Getting the MLD");
        MutableLiveData<List<NotificationContent>> notifs = notificationsViewModel.getNotifications();

        Log.d("NA", "Adding observer");
        notifs.observe(requireActivity(), new Observer<List<NotificationContent>>() {
            @Override
            public void onChanged(List<NotificationContent> notificationContents) {
                Log.d("ANDROIDAPP", String.format("onChanged: detected and done, item size %d", notificationContents.size()));
                adapter.updateNotifications(notificationContents);
            }
        });

        return binding.getRoot();
    }

执行代码 :

ViewModel

最后是 public class NotificationsViewModel extends ViewModel { private final MutableLiveData<List<NotificationContent>> notifications = new MutableLiveData<List<NotificationContent>>(); public MutableLiveData<List<NotificationContent>> getNotifications() { if (notifications.getValue() == null) { notifications.setValue(new ArrayList<NotificationContent>()); } return notifications; } public void removeNotification(NotificationContent notificationContent) { List<NotificationContent> n = getNotifications().getValue(); n.remove(notificationContent); notifications.setValue(n); } public void addNotification(NotificationContent notificationContent) { List<NotificationContent> n = getNotifications().getValue(); n.add(notificationContent); notifications.setValue(n); } public void addNotification(int position, NotificationContent notificationContent) { List<NotificationContent> n = getNotifications().getValue(); n.add(position, notificationContent); notifications.setValue(n); } } 的代码:

RecyclerView.Adapter

为了了解我的片段之间的相互作用,这里是我的 public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.ViewHolder> { public class ViewHolder extends RecyclerView.ViewHolder { // View Items declaration... public ViewHolder(View itemView) { super(itemView); // View items retrieval ... } } private List<NotificationContent> mNotifications = new ArrayList<>(); public void updateNotifications(List<NotificationContent> newNotifications) { Log.d("NA", "Clearing"); this.mNotifications.clear(); Log.d("NA", "Setting"); this.mNotifications = newNotifications; Log.d("NA", "NotifyChangedDataset"); this.notifyDataSetChanged(); } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // Some code ... } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { // Some code ... } @Override public int getItemCount() { return mNotifications.size(); } } 图形表示:

但问题是,通过 nav_graph.xml 添加项目后,它最终清空,这是代码中给出的日志中的日志:

addNotificationFragment
java android android-recyclerview android-livedata mutablelivedata
1个回答
0
投票

一旦你返回你的视图模型就被破坏了,所以你必须重新创建它, 所以你必须用构造函数初始化 MutableLiveData 对象及其值,所以当模型清晰时,就可以用这个模型重新分配你的值。

所以试试这个方法。

D/ANDROIDAPP: onViewCreated: addNotification 1
D/ANDROIDAPP: onChanged: detected and done, item size 1
D/NA: Clearing
D/NA: Setting
D/NA: NotifyChangedDataset
D/ANDROIDAPP: onViewCreated: addNotification 2
D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.etilawin.tgvmaxplanner activity: com.etilawin.tgvmaxplanner.MainActivity@df816d4
D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.etilawin.tgvmaxplanner activity: com.etilawin.tgvmaxplanner.MainActivity@df816d4
D/NA: New Adapter
D/NA: Setting adapter
D/NA: Setting layout
D/NA: Item Touche helper
D/NA: Button listener
D/NA: Getting the MLD
D/NA: Adding observer
D/ANDROIDAPP: onChanged: detected and done, item size 0
D/NA: Clearing
D/NA: Setting
D/NA: NotifyChangedDataset
© www.soinside.com 2019 - 2024. All rights reserved.