I have a ViewPager with the 10 images comes through webservices(JSON), At first ViewPager work smoothly (fine).
but When back from the activity and reopen it again I got this error :
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged!
Expected adapter item count: 10, found: 0 Pager id: com.akm.demo.activities:id/slideShowPager Pager class: class android.support.v4.view.ViewPager
In main Activity class
if (mSlideShowData != null) { // mSlideShowData is An ArrayList of the Images Which I pass to ImagePagerAdapter
adapter = new ImagePagerAdapter(getContext(), mSlideShowData);
slideShowPager.setAdapter(adapter);
//slideShowPager.invalidate();
((PagerAdapter)slideShowPager.getAdapter()).notifyDataSetChanged();
}
else{
Toast toast = Toast.makeText(getContext(), "No Record Found", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
ImagePagerAdapter.java
public class ImagePagerAdapter extends PagerAdapter {
private Context mContext;
private ImageLoader mImageLoader; // It's use for downloading the image from server
private ArrayList<SlideShowData> slideShowImages; // In this I have all the detail of the image from API
public ImagePagerAdapter(Context context, ArrayList<SlideShowData> mSlideShowData) {
mContext = context;
slideShowImages = mSlideShowData;
mImageLoader = new ImageLoader(mContext);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return slideShowImages.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageViewSlideShow = new ImageView(mContext);
int padding = mContext.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageViewSlideShow.setPadding(padding, padding, padding, padding);
imageViewSlideShow.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
mImageLoader.DisplayImage(ServiceURLs.URL+"/photos/"+slideShowImages.get(position).photo_name, R.drawable.loading, imageViewSlideShow);
((ViewPager) container).addView(imageViewSlideShow, 0);
return imageViewSlideShow;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
For solution , I tried :
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
//And
slideShowPager.getAdapter().notifyDataSetChanged();
the solution I got from stackoverflow but couldn't work.
what i need to changes for working ?
Solution
earlier I am Passing the ArrayList But this time String Array **(working )** Really I don't know whats the problem with the ArrayList
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…