Reputation: 1611
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
Upvotes: 12
Views: 15096
Reputation: 688
Reason of the problem the Pager can be refreshed during some measurement and in this case will refresh count of your adapter, if this count will not equals with saved count you will see the error. So my solution looks:
public abstract class ViewPagerCursorAdapter extends PagerAdapter {
private int mCount;
public ViewPagerCursorAdapter(Context ctx, Cursor cursor, int resource) {
super();
...
mCount = cursor.getCount();
...
}
@Override
public int getCount() {
return mCount;
}
public Cursor swapCursor(Cursor newCursor) {
...
mCount = newCursor.getCount();
notifyDataSetChanged();
}
}
Good luck.
Upvotes: 13
Reputation: 33
The answer they gave worked for a similar problem of mine.. I am just in production, have no idea how all these codes work, But what they said kind of helped me in the other thread of yours. You have to notify like they said, in the places where you set or delete your images.
So, I think here your problem is that you have to make a notify after you onCreateView() and also in your destroyItem() .. I would add the already suggested solution here.. I guess that should do it.. hope I am not misguiding you.. good luck..
slideShowPager.getAdapter().notifyDataSetChanged();
td:lr; That line in two of your functions onCreateView() and destroyItem() in the end might do the trick
Upvotes: -1