I have 2 requests that are sent using retrofit.

The first request sent, received a response in the onResponse method itself response.body - this is the list, but it is in the onResponse method.

I send 2 request and actually get an answer, also a list of response.body.

I can not understand How to add data to the list from which the adapter takes the data? Well, as far as I know, update it?

final Call<ArrayList<ProductModel>> call = (Call<ArrayList<ProductModel>>) service.getProducts("pagin.php","asda", 0); call.enqueue(new Callback<ArrayList<ProductModel>>() { @Override public void onResponse(Call<ArrayList<ProductModel>> call, Response<ArrayList<ProductModel>> response) { if (response.code() == 200) { for (ProductModel model : response.body()) { a[0] = a[0] + model.getNameProduct(); Log.d("myfirst",a[0]); adapter = new RecyclerAdapter(response.body(), ProductsActivity.this); rv.setAdapter(adapter); } 

    2 answers 2

    The adapter has an add() method, which, oddly enough, adds data to the adapter. Also, starting with a specific API , I don’t remember exactly which adapter has the addAll() method, which adds a whole collection of data to the adapter.
    Remember to call after adding notifyDataSetChanged()

    Field:

     private RecyclerAdapter adapter = null; 

    in onResponse :

     if(adapter == null) { adapter = new RecyclerAdapter (...); } else { adapter.addAll(response.body()); adapter.notifyDataSetChanged(); } 
    • and where should I prescribe this method? My adapter is created in the first request in onResponse - Martinez Toni
    • @MartinezToni, create a field in which to write a new adapter, if it is differently null , and if it is not equal - add data to the adapter - Vladyslav Matviienko
    • I have an adapter sheet? What crutch? - Martinez Toni
    • @MartinezToni, you yourself came up with this crutch, I did not speak about any sheet. - Vladyslav Matviienko
    • Well, I will say this, in the second request in the method I write adapter.add () - and it underlines add. - Martinez Toni
    1. You need to create a variable of type ArrayList<ProductModel> data = new ArrayList<> in the activation / fragment / singleton, i.e. where you run your task.
    2. When creating an activity / fragment, create an adapter and transfer this empty list there.
    3. When receiving data from the network, immediately add all the incoming data to the list and notify the adapter about it. To assign a new adapter many times is not necessary.

       call.enqueue(new Callback<ArrayList<ProductModel>>() { @Override public void onResponse(Call<ArrayList<ProductModel>> call, Response<ArrayList<ProductModel>> response) { if (response.code() == 200) { int prevSize = data.size(); data.addAll(response.body()); if(rv.getAdapter()==null){ rv.setAdapter(new RecyclerAdapter(data, ProductsActivity.this)); } rv.getAdapter().notifyItemRangeInserted(prevSize, data.size()); } } } 
    • The adapter is created after the first request and the list is transmitted not empty, but with the data received from the server - Martinez Toni
    • @MartinezToni, well, I changed the answer, nothing hinders you from initially creating an adapter with an empty list, and not creating it only after the data has arrived. - Yuriy SPb
    • I do not understand where this list? How do I create an adapter? for (ProductModel model: response.body ()) {adapter = new RecyclerAdapter (response.body (), ProductsActivity.this); rv.setAdapter (adapter); - Martinez Toni
    • @MartinezToni, I wrote to you that you are doing wrong. Reread my answer again. It says that the list should be in the activation and it is not necessary to create an adapter many times in a cycle. - Yuriy SPb
    • and what to do with the cycle then? - Martinez Toni