How to add data to the list from which the adapter collects data



  • I have two requests that go with retrofit.

    First request sent, answered onResponse response.body is the list, but it's onResponse.

    I'm sending two requests and I'm getting an answer, too, a response list.body.

    I can't understand how to add the data to the list from which the adapter takes the data?

    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);
            }
    



  • Adapter has a method. add()which, strangely, adds data to the adapter. Also starting with a certain APII don't remember exactly where the adapter has a method. addAll()which adds to the adapter a whole data collection.
    Don't forget to call you after addition. notifyDataSetChanged()

    Field:

    private RecyclerAdapter adapter = null;
    

    Total onResponse:

    if(adapter == null) {
        adapter = new RecyclerAdapter (...);
    } else {
        adapter.addAll(response.body());
        adapter.notifyDataSetChanged();
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2