我正在从我的活动中调用寻呼机适配器。我从 JSON 中获取数据并传递到 ArrayList mSlidedata
ImagePagerAdapter adapter = new ImagePagerAdapter(mContext, mSlideData);
slideShowPager.setAdapter(adapter);
slideShowPager.setCurrentItem(0);
ImagePagerAdapter 是我扩展 PagerAdapter 的类。这是代码:
public class ImagePagerAdapter extends PagerAdapter {
private Context mContext;
private ImageLoader mImageLoader;
private ArrayList<SlideData> slideShowImages;
public ImagePagerAdapter(Context context, ArrayList<SlideData> mSlideShowData) {
mContext = context;
slideShowImages = mSlideShowData;
mImageLoader = new ImageLoader(mContext);
}
@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+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);
}
}
这是我用来为 PagerAdapter 实现的所有代码
问题:当活动开始时,时间运行正常,但是当我返回并重新加载活动时再次错误
LogCat 详细信息:
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
替代解决方案:将 ArrayList 传递给字符串数组
for (int i = 0; i < mSlideData.size(); i++)
{
slideImagesArray[i] = ServiceURLs.URL+mSlideData.get(i).photo_name;
}
adapter = new ImagePagerAdapter(mContext, imagesStringArray);
slideShowPager.setAdapter(adapter);
slideShowPager.setCurrentItem(0);
工作正常,字符串数组没有问题。
那么,为什么在ArrayList和String Array工作正常期间会出现问题?