Devesh Agrawal
Devesh Agrawal

Reputation: 9212

adapter.notifyDataSetChanged() not working in android

This is my code, where i am setting adapter to value originalProductList which has 0 elements initially. later i am updating this field that prints 1595 (number of data items)

private List<ParentListItem> originalProductList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    recyclerView = (RecyclerView) findViewById(R.id.crime_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter = new MyAdapter(this, originalProductList);
    adapter.onRestoreInstanceState(savedInstanceState);
    recyclerView.setAdapter(adapter);

    getProducts();

}

private void getProducts() {
    if (Utility.getParentListItems().size() == 0) {
        final ProgressDialog loading = new ProgressDialog(ActivityProductList.this, R.style.MyTheme);
        loading.setCancelable(true);
        loading.show();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASERESTURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RestInterface service = retrofit.create(RestInterface.class);
        Call<List<Product>> call = service.getProducts();

        call.enqueue(new Callback<List<Product>>() {

            @Override
            public void onResponse(Response<List<Product>> response, Retrofit retrofit) {
                List<Product> products = response.body();
                loading.dismiss();


                //logic to parse data
                Utility.setParentListItems(parentListItems);
                originalProductList.clear();
                originalProductList.addAll(Utility.getParentListItems());
                Utility.displayToast("in fetch: " + originalProductList.size());  // this prints 1595
                adapter.notifyDataSetChanged();


            }
            @Override
            public void onFailure(Throwable t) {
                //Utility.displaySnackBar(coordinatorLayout, "INTERNET CONNECTION LOST");
                Utility.displayToast("some error");
                loading.dismiss();
            }
        });
    }
}

Calling this adapter.notifyDataSetChanged(); but it is not updating UI, no error also.

What is wrong in logic? How to fix this?

Upvotes: 1

Views: 750

Answers (2)

Mrugesh
Mrugesh

Reputation: 4517

@Override
public void onResponse(Response<List<Product>> response, Retrofit retrofit) {
    List<Product> products = response.body();
    loading.dismiss();

    //logic to parse data
    Utility.setParentListItems(parentListItems);
    originalProductList.clear();
    originalProductList.addAll(Utility.getParentListItems());
    madapter.addData(originalProductList);
}


public class Adapter {
    ...
    private List<Product> product;

    public void addData(List<Product> product) {
        this.product= product;
        notifyDataSetChanged();
    }
}

Upvotes: 3

Leonardo
Leonardo

Reputation: 3191

So to clearify my comment above, you should do something like this:

public class DebitAdapter extends RecyclerView.Adapter<DebitAdapter.DebitViewHolder> {

    private List<Debit> debits;

    public void setDebits(List<Debit> debits) {
        this.debits = debits;
        notifyDataSetChanged();
    }

    @Override
    public DebitViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(DebitViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return debits != null ? debits.size() : 0;
    }

    static class DebitViewHolder extends RecyclerView.ViewHolder {

        public DebitViewHolder(View itemView) {
            super(itemView);
        }
    }

}

And inside your callback, after fetching the list, something like this:

List<Product> products = response.body();
                loading.dismiss();


                //logic to parse data
                Utility.setParentListItems(parentListItems);
                originalProductList.clear();
                originalProductList.addAll(Utility.getParentListItems());
                Utility.displayToast("in fetch: " + originalProductList.size());  // this prints 1595
                adapter.setOriginalProductList(originalProductList);

Of course the above adapter is just an example on how to write an Adapter, following that you should have it full working.

Thanks

Upvotes: 0

Related Questions