我不明白为什么我的适配器没有更新我的 recyclerview

I can't figure out why my adapter is not updating my recyclerview

我已经阅读了一些类似的问题,并且我试图让它工作,但我不明白为什么我的适配器在 return 从我的 SaveItem activity 之后没有更新我的 recyclerview。所以我有两个选项卡:所有笔记和最喜欢的笔记。更具体地说:App image .

当我从底部按下浮动操作按钮时,它会启动一个新的 activity,我可以在其中记录新的笔记,但是当我 return 到 MainActivity 第一个片段时它没有更新我的回收视图:

这是我填充适配器的地方:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    adapter = new NoteAdapter(fillAdapter(),this,getActivity());
    recycler.setAdapter(adapter);
    recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
}

这是我尝试更新我的 recyclerview 的地方:

@Override
public void onResume() {
    super.onResume();
    adapter.addAll(fillAdapter());
    recycler.setAdapter(adapter);
}

这是我的 addAll 方法:

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolderNote> {
    // Other codes....
    public void addAll(List<Note> newNotes) {
        this.notes.clear();
        this.notes.addAll(newNotes);
        this.notifyDataSetChanged();
    }
}

在你的 Fragment class 中列出 Note

private List<Note> allNotes;

然后在你的onActivityCreated

里面
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    allNotes = fillAdapter();

    adapter = new NoteAdapter(allNotes, this, getActivity());
    recycler.setAdapter(adapter);
    recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
}

现在要更新您的列表,请再次调用 fillAdapter 函数以再次填充 allNotes 列表并像这样修改您的 onResume

@Override
public void onResume() {
    super.onResume();
    allNotes = fillAdapter();
    adapter.notifyDatasetChanged();
}

addAll 函数目前没有必要。从 NoteAdapter 中删除函数。

关键是要传递给适配器的列表的引用。