This is a repost of an answer I wrote on Stack Overflow.
There are two main things that I find helpful when exploring a new API: seeing examples (especially ones with pictures) and just trying out all the possibilities. This post will hopefully help you with that as you are learning how to update the adapter data in an Android RecyclerView. If you need help setting up the RecyclerView itself, then see this post or scroll down to the Code section below.
Overview
The process of modifying the adapter data includes two main steps every time:
- Update the data set
- Notify the adapter of the change
The following examples will show how to do that.
Insert single item
Add “Pig” at index 2
.
String item = "Pig";
int insertIndex = 2;
data.add(insertIndex, item);
adapter.notifyItemInserted(insertIndex);
Insert multiple items
Insert three more animals at index 2
.
ArrayList<String> items = new ArrayList<>()…