当我尝试删除项目并在ViewPager上滚动时,索引越界异常

时间:2016-12-13 10:10:16

标签: android android-viewpager android-adapter

我目前正在为我的某个应用程序使用垂直viewpager。我目前有一个任务是拍照并提供删除照片和重拍的选项。我能够捕捉并添加所有照片,但当我删除它时,该位置的项目消失但当我向下滚动时我得到indexoutofbounds异常。

这是我的适配器代码:

private class BaseAdapterImpl extends PagerAdapter {
    private View.OnClickListener mListener;

    private final SimpleDateFormat mDateFormat;

    private DisplayImageOptions options;


    private TreeMap<Long, List<WashingPhoto>> mPhotoMapping;

    private int mSize;

    private Long[] mKeys;

    List<List<WashingPhoto>> mPhotoList;

    public BaseAdapterImpl(View.OnClickListener listener, Map<Long, List<WashingPhoto>> photoMap) {
        //super(activity);

        mListener = listener;

        mDateFormat = new SimpleDateFormat("dd MMM yyyy");

        mPhotoMapping = new TreeMap<>(photoMap);



        mSize = mPhotoMapping.size();


        mKeys = mPhotoMapping.keySet().toArray(new Long[mSize]);




        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(false)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnLoading(R.drawable.ic_main_logo)
                .build();

    }

    @Override
    public int getCount() {
        return mSize;
    }


    public List<WashingPhoto> getItem(int position)
    {

        return mPhotoMapping.get(mKeys[position]);
    }
    public List<WashingPhoto> removeItem(int position)
    {
        return  mPhotoMapping.remove(mKeys[position]);
    }

     public void removeKeys(int position)
     {
ArrayList<Long> keys = new ArrayList<>(Arrays.asList(mKeys));
keys.remove(position);
      }



    @Override
    public Object instantiateItem(ViewGroup container, final int position)
    {
        View inflate = getActivity().getLayoutInflater().inflate(R.layout.page_3shot, null);
        container.addView(inflate);
        ViewUtils.vuFind(inflate, R.id.layout_during).setVisibility(View.INVISIBLE);

        mPhotoList = new ArrayList<List<WashingPhoto>>(mPhotoMapping.values());
        List<WashingPhoto> photos = mPhotoList.get(position);

        int photoSize = photos.size();
        if (photoSize > 0)
        {
            PhotoType type = mTypes.get(Integer.parseInt(photos.get(0).washing_photo_type_id));
            if (type != null)
            {
                ViewUtils.vuSetText(inflate, type.type_name, R.id.photo_subject_type_display);
            } else
            {
                ViewUtils.vuSetText(inflate, "", R.id.photo_subject_type_display);
            }


            ImageView imageView;

            for (int i = 0; i < photoSize; i++) {
                // int washingType = Integer.parseInt(photos.get(i).washing_photo_type_id);
                int washingType = photos.get(i).photo_type;

                switch (washingType) {
                    case 2:
                        imageView = (ImageView) inflate.findViewById(R.id.img_before);
                        imageView.setImageDrawable(null);


                        if (!TextUtils.isEmpty(photos.get(i).photo_url)) {

                            ImageLoader.getInstance().displayImage(photos.get(i).photo_url, imageView, options);
                        }
                        break;

                    case 4:
                        imageView = (ImageView) inflate.findViewById(R.id.img_after);
                        imageView.setImageDrawable(null);
                        if (!TextUtils.isEmpty(photos.get(i).photo_url)) {

                            ImageLoader.getInstance().displayImage(photos.get(i).photo_url, imageView, options);
                        }
                        break;
                }


                 }


        }
        inflate.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(final View view) {
                final  List<WashingPhoto> items = (List<WashingPhoto>) mAdapter.getItem(mPagerShot.getCurrentItem());

                new AlertDialog.Builder(getActivity()).
                        setMessage("Delete this photo?").
                        setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (items != null && items.size() > 0) {
                                    for (int i = 0; i < items.size(); i++) {
                                        WashingPhoto p = items.get(i);
                                        items.remove(i);
                                        mPhotos.remove(p);
                                        p.delete();
                                        i--;

                                    }
                                    mAdapter.removeItem(position);
                                    mAdapter.removeKeys(position);
                                    notifyDataSetChanged();


                                    if(mAdapter.getCount()<=0){

                                        mGroupId = 0;
                                        groupPhotos.clear();
                                        groupPhotos.keySet().clear();
                                    }
                                }

                            }
                        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create().show();

                return true;
            }
        });
        return inflate;
    }



    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView((LinearLayout) object);
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public int getItemPosition(Object object) {

        return PagerAdapter.POSITION_NONE;

    }

我尝试了这篇文章中提到的解决方案:[here] [1]

[1]:how to delete item from viewpager and pageradapter但我仍然得到了OOB例外。

1 个答案:

答案 0 :(得分:0)

在方法getCount中直接使用mPhotoMapping.size()。

@Override
public int getCount() {
    return mPhotoMapping.size();
}

从我删除照片时看到的内容中,您不会更新mSize,因此适配器知道它的项目数比实际更多。