这是我的代码,我将适配器设置为值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,也没有错误。
逻辑有什么问题?如何解决这个问题?
答案 0 :(得分:1)
@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();
}
}
答案 1 :(得分:0)
所以要清除上面的评论,你应该这样做:
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);
当然上面的适配器只是一个关于如何编写适配器的示例,之后你应该让它完全正常工作。
由于