java.lang.IndexOutOfBoundsException:索引1无效,从GridView中删除项目时大小为1

时间:2015-12-07 08:29:23

标签: android gridview indexoutofboundsexception android-gridview

我正在尝试在Button点击事件中删除GridView的多个选定项目 但它抛出了这个错误IndexOutOfBoundException

有人可以帮我解决这个问题^

错误日志

12-07 13:51:21.006  14416-14416/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.tazeen.classnkk, PID: 14416
    java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.remove(ArrayList.java:403)
            at com.example.tazeen.classnkk.AddPost$2.onClick(AddPost.java:244)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19748)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

活动代码

imgDeleteImagesFromGallery.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {

                    ArrayList<String> selectedItems = gridAdapter.getCheckedItems();
                    for (int i = 0; i < selectedItems.size(); i++)
                    {

                            fileName.remove(i);

                    }gridAdapter.notifyDataSetChanged();}
            });

这是我的GridView Adapter类

 class GridView_Adapter extends BaseAdapter {

        private LayoutInflater mInflater;
        SparseBooleanArray mSparseBooleanArray;
        public GridView_Adapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mSparseBooleanArray = new SparseBooleanArray();
        }

            public ArrayList<String> getCheckedItems() {
            ArrayList<String> mTempArry = new ArrayList<String>();

                for(int i=0;i<fileName.size();i++) {
                if(mSparseBooleanArray.get(i)) {
                    mTempArry.add(fileName.get(i));
                }}
            return mTempArry;
        }

        public int getCount() {
            return fileName.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
           ViewHolder holder;
            if (convertView == null)
            {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.add_post_grid_item_layout, parent , false);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            Bitmap myBitmap = BitmapFactory.decodeFile(fileName.get(position));
            holder.image.setImageBitmap(myBitmap);

            holder.checkbox.setTag(position);
            holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                 {
                     mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
                     int pos = (Integer) buttonView.getTag();
                     if (!buttonView.isChecked())
                     {
                         picsName.remove((String) fileName.get(pos));
                     }
                     else if(buttonView.isChecked())
                     {
                         if (!picsName.contains((String) fileName.get(pos))) {
                             picsName.add((String) fileName.get(pos));

                         }
                     }
                     notifyDataSetChanged();
                 }
             });


            return convertView;
        }

1 个答案:

答案 0 :(得分:3)

您的循环会导致此错误,因为它会首先删除较低的索引 它应该是相反的:从最高索引开始删除那个并返回到较低的索引。

这样的事情:

int len = selectedItems.size() - 1; // Precalculate it once, so that the cycle runs faster
for (int i = len; i > 1; i--)
{

        fileName.remove(i);

}