Java 将其他活动中的项目添加到recyclerview的最佳方法?

Java 将其他活动中的项目添加到recyclerview的最佳方法?,java,android,android-recyclerview,Java,Android,Android Recyclerview,因此,我在MainActivity中有一个recyclerview,希望打开一个窗口(新活动?),并从该窗口向我的recyclerview添加项目。现在我正试着这样做。问题是,当我将该项添加到列表中时,我的recyclerview不会显示它(我猜这是因为我没有更新适配器?),这样做的最佳方式是什么?我不介意改变我的程序结构 MainActivity.java static ArrayList<filterItem> filters = new ArrayList<

因此,我在MainActivity中有一个recyclerview,希望打开一个窗口(新活动?),并从该窗口向我的recyclerview添加项目。现在我正试着这样做。问题是,当我将该项添加到列表中时,我的recyclerview不会显示它(我猜这是因为我没有更新适配器?),这样做的最佳方式是什么?我不介意改变我的程序结构

MainActivity.java

        static ArrayList<filterItem> filters = new ArrayList<>();

        filters.add(new filterItem("1", R.mipmap.ic_launcher));
        filters.add(new filterItem("2", R.mipmap.ic_launcher));
        filters.add(new filterItem("3", R.mipmap.ic_launcher));
        filters.add(new filterItem("4", R.mipmap.ic_launcher));
        filters.add(new filterItem("5", R.mipmap.ic_launcher));
        filters.add(new filterItem("6", R.mipmap.ic_launcher));
        filters.add(new filterItem("7", R.mipmap.ic_launcher));
        filters.add(new filterItem("8", R.mipmap.ic_launcher));
        filters.add(new filterItem("9", R.mipmap.ic_launcher));
        filters.add(new filterItem("10", R.mipmap.ic_launcher));

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new MyAdapter(this, filters);
        adapter.setClickListener(this);
        recyclerView.setAdapter(adapter);
static ArrayList filters=new ArrayList();
添加(新的filterItem(“1”,R.mipmap.ic_启动器));
添加(新的filterItem(“2”,R.mipmap.ic_启动器));
添加(新的filterItem(“3”,R.mipmap.ic_启动器));
添加(新的filterItem(“4”,R.mipmap.ic_启动器));
添加(新的filterItem(“5”,R.mipmap.ic_启动器));
添加(新的filterItem(“6”,R.mipmap.ic_启动器));
添加(新的filterItem(“7”,R.mipmap.ic_启动器));
添加(新的filterItem(“8”,R.mipmap.ic_启动器));
添加(新的filterItem(“9”,R.mipmap.ic_启动器));
添加(新的filterItem(“10”,R.mipmap.ic_启动器));
RecyclerView RecyclerView=findViewById(R.id.RecyclerView);
recyclerView.setLayoutManager(layoutManager);
adapter=新的MyAdapter(此为过滤器);
adapter.setClickListener(此);
recyclerView.setAdapter(适配器);
BrowseFilters.java
MainActivity.filters.add(FilterLoadd)


我知道已经有帖子回答了如何做到这一点的问题,答案说只是让列表像我所做的那样保持静态。但这是最好的方法还是有更好的方法?如果制作一个静态列表是最好的方法,那么我如何在MainActivity中显示我的更新列表?

您需要在将项目添加到适配器后调用
适配器。notifyDataSetChanged()

这种情况下的最佳实践是来自MainActivity的
startActivityForResult()

在第二个活动中,将要添加到MainActivity中的额外项目传递回MainActivity

在main活动中,重写activityResult()
并处理将额外项添加到适配器中的操作,然后调用
adapter.notifyDataSetChanged()