0

I am looking for the solution to my problem about how to refresh adapter without re-setting my RecyclerView adapter. Through this answer I have done but nothing work what am I do wrong. Here is my code that set recyclerView adapter.

public class OrderedFoodsFragment extends Fragment
{
private NewInvoice invoice;
private List<InvoiceItem> items= new ArrayList<>();
private OrderedAdapter adapter;
public OrderedFoodsFragment(NewInvoice invoice)
{
    this.invoice = invoice;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_ordered_foods, container, false);
    initializeComponents(view);
    return  view;
}

private void initializeComponents(View view)
{
    RecyclerView rvOrderedFoods = (RecyclerView) view.findViewById(R.id.rvOrderedFoods);
    adapter= new OrderedAdapter(getActivity(), items,R.layout.ordered_layout);
    rvOrderedFoods.setAdapter(adapter);
    setInvoiceItems();
}

private void setInvoiceItems()
{
    IInvoiceApi api= ApiClient.getClientWithAuthorization(App.getContext()).create(IInvoiceApi.class);
    Call<List<InvoiceItem>> call= api.getInvoiceItems(invoice.getId());
    call.enqueue(new Callback<List<InvoiceItem>>()
    {
        @Override
        public void onResponse(@NonNull Call<List<InvoiceItem>> call, @NonNull Response<List<InvoiceItem>> response)
        {
            if (response.isSuccessful())
            {
                //My comment code
                //items=response.body();
                //adapter= new OrderedAdapter(getActivity(), items,R.layout.ordered_layout);
                //noinspection ConstantConditions
                items.clear();
                //noinspection ConstantConditions
                items.addAll(response.body());
                adapter.notifyDataSetChanged();
                Log.i("InvoiceData", adapter.getItemAtPosition(0).getName());
            }
            else if (response.code() == 401)
            {
                new AlertDialog.Builder(getActivity()).setTitle("Session Expire").setMessage("សិទ្ធក្នុងការប្រើកម្មវិធីអស់ពេល").show();
                App.promptUnauthorized(getActivity());
            }
            else
            {
                items= new ArrayList<>();
            }
        }

        @Override
        public void onFailure(@NonNull Call<List<InvoiceItem>> call, @NonNull Throwable t)
        {
            call.cancel();
        }
    });
}

}

I'm using retrofit 2.0 to request for my data. And the response.body() has my data. But the recyclerview not show the data. But if I re-set the adapter in the onRespose method to the response.body() as my comment code in this method it works but I don't want it like. Any solution help me.

Sokheang Khun
  • 137
  • 1
  • 2
  • 11

1 Answers1

0

You have to set this new list to the adapter.

You can create a setDataSet() method in your adapter and call it before notifydatasetchanged

Add below method in adapter

public void setDataSet(List<InvoiceItem> items) {
    this.items= items;
}

and call it before notifydatasetChanged like this:

adapter.setDataSet(items);
adapter.notifyDataSetChanged();
Swati
  • 1,179
  • 9
  • 28