![](/img/trans.png)
[英]adapter.notifyDataSetChanged() not working in android
[英]adapter.notifyDataSetChanged() is not working in recyclerview in android
public void serverCall(String Id, final String title, final String descrpiton) {
ReUseComponets serverCall = new ReUseComponets(new ShopProductlisteners() {
@Override
public void showShopProductListnerResult(List<ShopProductModel> result) {
if (result != null) {
if(adapter==null) {
List<ShopProductModel> shopProductModelList = result;
recyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
adapter = new ShopSubCateogryAdapter(getActivity(), shopProductModelList, title, descrpiton);
emptytextView.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
} else {
emptytextView.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
progressDialog.dismiss();
}
@Override
public void showError(Throwable error) {
progressDialog.dismiss();
}
});
serverCall.asyncPostShopProduct(Id, "en_IN");
}
这是我的服务器呼叫代码
@Override
public void onReceived(int requestCode, int resultCode, Intent data) {
Toast.makeText(getActivity(), "Click", Toast.LENGTH_LONG).show();
String title = this.getArguments().getString(Constants.TITLE);
String id = this.getArguments().getString(Constants.ID);
String descrption = this.getArguments().getString(Constants.DESCRIPTION);
serverCall(id, title, descrption);
adapter.notifyDataSetChanged();
}
使用此代码,我尝试调用服务器调用。 当我从下一个活动回到上一个片段时,则我的数据列表未在适配器中更新。 有人可以建议我做错了什么吗?
afaik, notifyDataSetChanged()
仅在Adapter类中起作用。 因此,您可以通过添加一种方法来更改适配器中的所有项目列表来解决该问题。 这样的事情(这里我们使用swap()
方法):
public class ShopSubCateogryAdapter extends
RecyclerView.Adapter<ShopSubCateogryAdapter.ViewHolder> {
...
private List<ShopProductModel> shopProductModelList;
public void swap(List<ShopProductModel> shopProductModelList) {
// use this. to refer your variable with class scope
// if your parameter has the same name with the variable class scope.
this.shopProductModelList.clear();
this.shopProductModelList.addAll(shopProductModelList);
notifyDataSetChanged();
}
..
}
然后,您可以将其用于:
adapter.swap(result);
如果您的适配器不为null,则您什么也不做。 您需要更新列表,然后调用notifyDataSetChanged()
像这样更新您的方法 :
public void serverCall(String Id, final String title, final String descrpiton) {
ReUseComponets serverCall = new ReUseComponets(new ShopProductlisteners() {
@Override
public void showShopProductListnerResult(List<ShopProductModel> result) {
if (result != null) {
if(adapter==null) {
//shopProductModelList initialize it globally
List<ShopProductModel> shopProductModelList = result;
recyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
adapter = new ShopSubCateogryAdapter(getActivity(), shopProductModelList, title, descrpiton);
emptytextView.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else{
// do what you want to add to your list like this:
// if you intialize shopProductModelList it globally then you can call it here
shopProductModelList.clear();
shopProductModelList = result;
adapter.notifyDataSetChanged();
}
} else {
emptytextView.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
progressDialog.dismiss();
}
并从onReceived()方法中删除此行
@Override
public void onReceived(int requestCode, int resultCode, Intent data) {
Toast.makeText(getActivity(), "Click", Toast.LENGTH_LONG).show();
String title = this.getArguments().getString(Constants.TITLE);
String id = this.getArguments().getString(Constants.ID);
String descrption = this.getArguments().getString(Constants.DESCRIPTION);
serverCall(id, title, descrption);
// no need this line here.
//adapter.notifyDataSetChanged();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.