如何删除View分页器的特定位置

时间:2019-04-10 09:16:02

标签: java android android-viewpager adapter

我在Android应用中实现了ViewPager。我想在其中添加一些图像,一切正常(添加图像),但是当我要删除图像时,它正在工作并删除该图像,但是删除最后一个图像而不是要删除的特定图像,我不不知道如何获取特定图像的位置并将其删除。

public class PhotoAdd_Adapter extends PagerAdapter {
    private LayoutInflater inflater;
    private Context context;
    ArrayList<String> mylist;
    SharedPreferences sharedPreferences;
    JSONArray responsearray = null;
    private static final String TAG_PHOTO_ONE = "Gallery_Full";
    HashMap<String, String> get;
    LinearLayout addimages;
    String ImageID;
    ProgressBar progressBar;
    String finalimgpath, imageId;
    ImageView imageView, deletebtn;
    TextView no_image, p_id;
    private FragmentManager f_manager;
    int delPosition;

    public PhotoAdd_Adapter(Context context, ArrayList<String> mylist, FragmentManager f_manager) {
        this.f_manager = f_manager;
        this.context = context;
        this.mylist = mylist;
        inflater = LayoutInflater.from(context);
        sharedPreferences = context.getSharedPreferences(context.getString(R.string.prefs), 0);
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
    @Override
    public int getCount() {
        return mylist.size();
    }
    @Override
    public int getItemPosition(@NonNull Object object) {
        if(mylist.contains(object)){
            return mylist.indexOf(object);
        }else {
            return POSITION_NONE;
        }
//        return super.getItemPosition(object);
//        return PagerAdapter.POSITION_NONE;
    }
    @Override
    public Object instantiateItem(ViewGroup view, final int position) {
        View imageLayout = inflater.inflate(R.layout.photoadd_adapter, view, false);
        assert imageLayout != null;
        imageView = imageLayout.findViewById(R.id.image_adapter);
        deletebtn = imageLayout.findViewById(R.id.deletebtn);
        p_id = imageLayout.findViewById(R.id.p_id);
        this.delPosition = position;       
        String test = String.valueOf(mylist.get(position));
        String finalimgpaths = context.getString(R.string.moreimgpath) + test;
        Glide.with(context)
                .load(finalimgpaths)
                .into(imageView);
        view.addView(imageLayout, 0);
        deletebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                showDialogBox();
                imageId = ImageID;
                Log.e("IMAGEIDONE", imageId);
                mylist.remove(position);
                notifyDataSetChanged();
                responsearray = responsearray(responsearray, position);
                new DeleteImage().execute(true);
            }
        });
        return imageLayout;
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }
    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }
    @Override
    public Parcelable saveState() {
        return null;
    }
    class DeleteImage extends AsyncTask<Boolean, Void, String> {
        @Override
        protected String doInBackground(Boolean... booleans) {
            ProfileApi profileApi = new ProfileApi();
            String result = null;
            try {
                Log.e("IMAGEID", ImageID);
                result = profileApi.deleteImage(sharedPreferences.getString("id", ""), imageId);
                Log.e("DELETERESULT", result);
                JSONObject object = new JSONObject(result);
                if (object.getString("error").equalsIgnoreCase("false")) {

                    return "true";
                } else {
                    String errormsg = object.getString("response");
                    return errormsg;
                }
            } catch (ApiException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String s) {
//            ad.hide();
            super.onPostExecute(s);
            try {
                if (s != null) {
                    Toast.makeText(context, "Image Deleted", Toast.LENGTH_SHORT).show();
                }
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }
    }
    public static JSONArray responsearray(JSONArray jarray, int pos) {
        JSONArray Njarray = new JSONArray();
        try {
            for (int i = 0; i < jarray.length(); i++) {
                if (i != pos)
                    Njarray.put(jarray.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Njarray;
    }
}

2 个答案:

答案 0 :(得分:0)

您的问题是在删除图像任务完成之前,重新加载了视图寻呼机。因此,将加载同一组图像,但由于已修改列表大小,因此不会加载最后一个图像。

解决方案: 等待删除操作完成,然后在ViewPager上调用

上的notifyDataSetChanged()

答案 1 :(得分:0)

我已经更新了您的代码,只需尝试以下代码:

 public class PhotoAdd_Adapter extends PagerAdapter {
        private LayoutInflater inflater;
        private Context context;
        ArrayList<String> mylist;
        SharedPreferences sharedPreferences;
        JSONArray responsearray = null;
        private static final String TAG_PHOTO_ONE = "Gallery_Full";
        HashMap<String, String> get;
        LinearLayout addimages;
        String ImageID;
        ProgressBar progressBar;
        String finalimgpath, imageId;
        ImageView imageView, deletebtn;
        TextView no_image, p_id;
        private FragmentManager f_manager;
        int delPosition;

        public PhotoAdd_Adapter(Context context, ArrayList<String> mylist, FragmentManager f_manager) {
            this.f_manager = f_manager;
            this.context = context;
            this.mylist = mylist;
            inflater = LayoutInflater.from(context);
            sharedPreferences = context.getSharedPreferences(context.getString(R.string.prefs), 0);
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
        @Override
        public int getCount() {
            return mylist.size();
        }
        @Override
        public int getItemPosition(@NonNull Object object) {
            if(mylist.contains(object)){
                return mylist.indexOf(object);
            }else {
                return POSITION_NONE;
            }
    //        return super.getItemPosition(object);
    //        return PagerAdapter.POSITION_NONE;
        }
        @Override
        public Object instantiateItem(ViewGroup view, final int position) {
            View imageLayout = inflater.inflate(R.layout.photoadd_adapter, view, false);
            assert imageLayout != null;
            imageView = imageLayout.findViewById(R.id.image_adapter);
            deletebtn = imageLayout.findViewById(R.id.deletebtn);
            p_id = imageLayout.findViewById(R.id.p_id);
            this.delPosition = position;       
            String test = String.valueOf(mylist.get(position));
            String finalimgpaths = context.getString(R.string.moreimgpath) + test;
            Glide.with(context)
                    .load(finalimgpaths)
                    .into(imageView);
            view.addView(imageLayout, 0);
            deletebtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    //                showDialogBox();
                    imageId = ImageID;
                    Log.e("IMAGEIDONE", imageId);
                    responsearray = responsearray(responsearray, position);
                    new DeleteImage().execute(true);
                }
            });
            return imageLayout;
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }
        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }
        @Override
        public Parcelable saveState() {
            return null;
        }
        class DeleteImage extends AsyncTask<Boolean, Void, String> {
            @Override
            protected String doInBackground(Boolean... booleans) {
                ProfileApi profileApi = new ProfileApi();
                String result = null;
                try {
                    Log.e("IMAGEID", ImageID);
                    result = profileApi.deleteImage(sharedPreferences.getString("id", ""), imageId);
                    Log.e("DELETERESULT", result);
                    JSONObject object = new JSONObject(result);
                    if (object.getString("error").equalsIgnoreCase("false")) {

                        return "true";
                    } else {
                        String errormsg = object.getString("response");
                        return errormsg;
                    }
                } catch (ApiException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(String s) {
    //            ad.hide();
                super.onPostExecute(s);
                try {
                    if (s != null) {
                        Toast.makeText(context, "Image Deleted",Toast.LENGTH_SHORT).show();
                    mylist.remove(position);
                    notifyDataSetChanged();
                    }
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
            }
        }
        public static JSONArray responsearray(JSONArray jarray, int pos) {
            JSONArray Njarray = new JSONArray();
            try {
                for (int i = 0; i < jarray.length(); i++) {
                    if (i != pos)
                        Njarray.put(jarray.get(i));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return Njarray;
        }
    }