Hello stackoverflow我试图开发一个显示来自SD卡和捕获的相机的图像的应用程序,并允许用户使用复选框删除图像。我可以通过复选框显示来自SD卡和相机的图像,但是我无法删除用户动态勾选的特定或多个图像。这是我的MainActivity.java
imgDeleteImagesFromGallery = (ImageView) findViewById(R.id.imgDeleteImage);
imgDeleteImagesFromGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
ArrayList<String> selectedItems = gridAdapter.getCheckedItems();
Log.e("data "," size = "+imgItemArray.size() + " & selectedItems size = " +selectedItems.size() + "Total photos selected: "+selectedItems.size());
Log.e(AddPost.class.getSimpleName(), "Selected Items: " + selectedItems.toString() + " Data Selected Items: "+ imgItemArray.toString());
for(int i = 0 ; i < selectedItems.size()-1 ; i++)
{
imgItemArray.remove(i);
}
gridAdapter.notifyDataSetChanged();
}
});
这是来自适配器类
的getView()方法 @Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
final ViewHolder holder;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) row.findViewById(R.id.image);
holder.checkbox = (CheckBox) row.findViewById(R.id.itemCheckBox);
row.setTag(holder);
}
else
{
holder = (ViewHolder) row.getTag();
}
final BitmapItems item = imgItemArray.get(position);
holder.image.setImageBitmap(item.getBitmap());
holder.checkbox.setTag(position);
holder.checkbox.setChecked(mSparseBooleanArray.get(position));
holder.checkbox.setOnCheckedChangeListener(mCheckedChangeListener);
return row;
}
CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// TODO Auto-generated method stub
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
Log.e("Is ", " Checked !!!");
imgItemArray.remove((Integer) buttonView.getTag());
}
};
当我勾选前2个图像进行删除时,会出现 java.lang.IndexOutOfBoundsException错误:索引1无效,大小为1
for(int i = 0 ; i < selectedItems.size()-1 ; i++)
{
imgItemArray.remove(i);
}
这是我的日志猫信息
10-27 10:43:31.945 4375-4375/com.example.tazeen.classnkk E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tazeen.classnkk, PID: 4375
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:248)
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)
提前致谢。
答案 0 :(得分:0)
当你从ArrayList中删除第一个元素时,它的大小减少到1.现在,arraylist中的最大索引变为0.尝试使用此代码:
for(int i = selectedItems.size()-1 ; i >= 0 ; i--)
{
imgItemArray.remove(i);
}
编辑:
您需要更改gridAdapter。它不应该返回所选项的ArrayList,它应该返回所选索引的arraylist。然后,您的selectedItems
列表将包含所选索引。在此之后,请执行:
for(int i = selectedItems.size()-1 ; i >= 0 ; i--)
{
imgItemArray.remove(selectedItems.get(i));
}