0

I am currently using a vertical viewpager for one of my applications. I currently have a task to take a photo and give an option to delete the photo and retake. I am able to capture and add all photos, but when i delete it, the item in that position disappears but when i scroll down i get the indexoutofbounds exception.

This is my code for the adapter:

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;

    }

I tried the solution mentioned in this post:[here][1]

[1]: how to delete item from viewpager and pageradapter and still I do get the OOB exception.

Community
  • 1
  • 1
nexus
  • 119
  • 13

1 Answers1

0

In the method getCount use directly the mPhotoMapping.size().

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

From what I can see when you delete the photo, you don't update mSize so the adapter knows that it has more items than it actually does.

bogdanN
  • 183
  • 10