传递ArrayList得到的错误和字符串数组在PagerAdapter中正常工作?

时间:2013-07-30 07:32:34

标签: android android-pageradapter

我从我的Activity中调用了一个寻呼机适配器。我从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);

工作正常从String Array中没有问题。

那么,为什么在 ArrayList String Array 期间出现问题 ??

0 个答案:

没有答案