-1

I usually used to add an item in a listview like this

            Name name = new Name("Via Button");
            nameList.add(name);
            mAdapter.notifyDataSetChanged();

I would like to know the equivalent of this in RecyclerView. Thanks a lot.

user1903022
  • 995
  • 1
  • 10
  • 19

1 Answers1

0

You can define the recycler view adapter which takes take a list as one of the params...in your case List...once you have defined the adapter you can pass on the list which is of class level to this adapter. once you fetch the data (through an api call or any other means) you can add add all the data and call notify data set to your custom adapter. Example:

// custructor of your custom adapter
public CustomAdapter(List<Name> myList) {
    this.myList = myList;
}

//your class implementation
public class myClass{
private ArrayList<Name> myList = new ArrayList<>();
CustomAdapter myAdapter;
.
.
.
public void init(){
 //more code and init of components

 myAdapter = new CustomAdapter(myList);
 recyclerView.setAdapter(myAdapter);
}


  //more codes
//once you receive data from api call (maybe)
Name name = new Name("Via Button");
        myList.add(name);
 myAdapter.notifyDatasetchanged();
//more codes
}