我已经看过几个关于这个问题的问题,我已经尝试添加建议的onNotifyDataChanged,但似乎没有什么对我有用。我将感谢任何帮助,下面是我的班级和内部类MyViewPagerAdapter,我该如何解决此问题
java.lang.IllegalStateException:应用程序的PagerAdapter 无需调用就更改了适配器的内容 PagerAdapter#notifyDataSetChanged!预期的适配器项目数:0,
代码:
public class SlideShowFragment extends DialogFragment {
private final String TAG = SlideShowFragment.class.getSimpleName();
private ArrayList<Image> images;
private ProgressBar pd;
/**
* a simple newInstance
*
* @return slideShowFragment
*/
public static SlideShowFragment newInstance() {
SlideShowFragment slideShowFragment = new SlideShowFragment();
return slideShowFragment;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_slideshow_dialog_fragement, container, false);
ViewPager viewPager = v.findViewById(R.id.viewpager);
assert getArguments() != null;
images = (ArrayList<Image>) getArguments().getSerializable("images");
int selectedPosition = getArguments().getInt("position");
pd = v.findViewById(R.id.img_progress);
Log.e(TAG, "position: " + selectedPosition);
Log.e(TAG, "images size: " + images.size());
MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter();
//added this incase of crash #FIXME 5/25/2018
myViewPagerAdapter.notifyDataSetChanged();
viewPager.setAdapter(myViewPagerAdapter);
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
/**
* myViewPager Adapter inner class
* I am using the Glide Library to help me display
* images from the json data on my server.
*/
class MyViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
MyViewPagerAdapter() {
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) Objects.requireNonNull(getActivity()).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert layoutInflater != null;
View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);
try {
ImageView imageViewPreview = view.findViewById(R.id.image_preview);
Image image = images.get(position);
Glide.with(getActivity()).load(image.getLarge())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.error(R.drawable.reload)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
pd.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target,
boolean isFromMemoryCache, boolean isFirstResource) {
pd.setVisibility(View.INVISIBLE);
return false;
}
})
.into(imageViewPreview);
// pd.setVisibility(View.INVISIBLE);
container.addView(view);
}catch (ExecutionError e){
Toast.makeText(getContext(),"Please Check Connection", Toast.LENGTH_SHORT).show();
}
return view;
}
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object obj) {
return view == obj;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
}
答案 0 :(得分:0)
在我的观点中,如果您正在使用适配器并在该适配器中分配了一个空的或一些基本数据集,那么您又要在相同的数据集中添加更多数据,并希望更新布局而不将其加载回然后你可以使用“notifyDataChanges”这个方法自动加载adpater中额外添加的数据,或者只是由用户更新。
例如:
private RecyclerView recyclerView;
List<Home_Data> ModelHomeData = new ArrayList<>(); //taking a empty object of list to show
recyclerView = (RecyclerView) view.findViewById(R.id.layout_modelBikeLinear);
recyclerView.setAdapter(new ModelAdapter(ModelDataForView, R.layout.template_models , getContext()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
在这种情况下,我们已经为视图分配了一个空的适配器,但我想添加更多数据,然后更改视图而不再一次加载它,所以现在我可以在列表数据中添加数据然后将使用“notifyDataChanged”
for(int i = 0; i < response.length(); i++){
//SliderUtils sliderUtils = new SliderUtils();
Model_Data DataModelFile = new Model_Data();
try {
JSONObject jsonObject = response.getJSONObject(i);
DataModelFile.setModelId(jsonObject.getString("id"));
DataModelFile.setModelName(jsonObject.getString("name"));
DataModelFile.setModelPrice(jsonObject.getString("price"));
DataModelFile.setBikeImageUrl(jsonObject.getString("image"));
if ((Integer.parseInt(jsonObject.getString("available")) > 0) && (Integer.parseInt(jsonObject.getString("show")) > 0)) {
DataModelFile.setBikeAvailable(true);
}
else
{
DataModelFile.setBikeAvailable(false);
}
} catch (JSONException e) {
e.printStackTrace();
}
ModelDataForView.add(DataModelFile);
}
adapter.notifyDataSetChanged();