Search code examples
javaandroidandroid-recyclerviewandroid-viewholder

How can I run a for loop through a recycler view in android?


I have a recycler view in my application, and instead of executing an onclick listener, I want to click a button outside of the recycler view, which instructs the app to go through all the items in my recycler view and perform operations on items. How can I do that?


Solution

  • You can add a public method in your adapter to get the list

    public List<Model> getList(){
        return modelList;
    }
    

    Get the list anywhere

    List<Model> listToModify = adapter.getList();
    

    Loop through the list and modify it, then set it to the adapter or call notifyDataSetChanged();