gpt4 book ai didi

android - adapter.notifyDataSetChanged() 在 android 中不工作

转载 作者:行者123 更新时间:2023-11-29 19:50:01 25 4
gpt4 key购买 nike

这是我的代码,我将适配器设置为值 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();
}
});
}
}

调用此适配器.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();
}
}

关于android - adapter.notifyDataSetChanged() 在 android 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com