繁体   English   中英

adapter.notifyDataSetChanged()在android中不起作用

[英]adapter.notifyDataSetChanged() not working in android

这是我的代码,我在其中将适配器设置为值originalProductList,该元素最初具有0个元素。 稍后,我将更新此打印1595(数据项数)的字段

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

调用此adapter.notifyDataSetChanged(); 但它没有更新UI,也没有错误。

逻辑上有什么问题? 如何解决这个问题?

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

因此,为了澄清我在上面的评论,您应该执行以下操作:

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

}

在回调中,获取列表后,如下所示:

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

当然,上面的适配器只是有关如何编写适配器的一个示例,随后您应该使其完全正常工作。

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM