0

I created an imageviewer app. Its working fine but when I select a gridview item to delete it, it stops working. On selecting the item, delete button appears . On clicking the delete button ,an alert dialog appears asking for confirmation.I want the file to get deleted after cliking the YES button. But it does not happen. Instead it throws an indexoutofbounds exception. How can I delete selected file succesfully ?

PhotosActivity.java

import static com.example.dell_1.myapp3.ImageViewer.ImageGallery.al_images;

public class PhotosActivity extends AppCompatActivity {
    int int_position;
    private GridView gridView;
    GridViewAdapter adapter;
    ArrayList<Model_images> al_menu = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_gallery);

        final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
        final ImageButton button2 = (ImageButton) findViewById(R.id.button2);
        final ImageButton button3 = (ImageButton) findViewById(R.id.button3);
        final ImageButton button4 = (ImageButton) findViewById(R.id.button4);
        final ImageButton button5 = (ImageButton) findViewById(R.id.button5);
        button1.setVisibility(View.GONE);
        button2.setVisibility(View.GONE);
        button3.setVisibility(View.GONE);
        button4.setVisibility(View.GONE);
        button5.setVisibility(View.GONE);

        gridView = (GridView) findViewById(android.R.id.list);
        int_position = getIntent().getIntExtra("value", 0);
        adapter = new GridViewAdapter(this, al_images, int_position);
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String abc = "file://" + al_images.get(int_position).getAl_imagepath().get(position);

                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                i.putExtra("id", position);
                i.putExtra("folderPosition", int_position);
                i.putExtra("abc", abc);
                startActivity(i);
            }
        });

        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                for (int j = 0; j < parent.getChildCount(); j++)
                    parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);

                // change the background color of the selected element
                view.setBackgroundColor(Color.LTGRAY);
                button1.setVisibility(View.VISIBLE);
                button2.setVisibility(View.VISIBLE);
                button3.setVisibility(View.VISIBLE);
                button4.setVisibility(View.VISIBLE);
                button5.setVisibility(View.VISIBLE);
                button3.setOnClickListener(
                        new View.OnClickListener() {
                            public void onClick(View view) {
                                AlertDialog.Builder builder1 = new AlertDialog.Builder(PhotosActivity.this);
                                builder1.setMessage("Are you sure you want to delete it ?");
                                builder1.setCancelable(true);

                                builder1.setPositiveButton(
                                        "Yes",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                File file = new File( al_menu.get(int_position).getAl_imagepath().get(position));
                                                file.delete();
                                               al_menu.remove(position);
                                                adapter.notifyDataSetChanged();
                                            }
                                        });

                                builder1.setNegativeButton(
                                        "No",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                dialog.cancel();
                                            }
                                        });

                                AlertDialog alert11 = builder1.create();
                                alert11.show();
                            }
                        });
                return true;
            }
        });


    }
}

ImageGallery.java:

public class ImageGallery extends AppCompatActivity {
    public static ArrayList<Model_images> al_images = new ArrayList<>();
    boolean boolean_folder;
    Adapter_PhotosFolder obj_adapter;
    GridView gv_folder;
    private static final int REQUEST_PERMISSIONS = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_gallery);
        gv_folder = (GridView) findViewById(android.R.id.list);

        final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
        final ImageButton button2 = (ImageButton) findViewById(R.id.button2);
        final ImageButton button3 = (ImageButton) findViewById(R.id.button3);
        final ImageButton button4 = (ImageButton) findViewById(R.id.button4);
        final ImageButton button5 = (ImageButton) findViewById(R.id.button5);
        button1.setVisibility(View.GONE);
        button2.setVisibility(View.GONE);
        button3.setVisibility(View.GONE);
        button4.setVisibility(View.GONE);
        button5.setVisibility(View.GONE);

        gv_folder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getApplicationContext(), PhotosActivity.class);
                intent.putExtra("value",i);
                startActivity(intent);
            }
        });

GridViewAdapter.java:

public class GridViewAdapter extends ArrayAdapter<Model_images> {

    Context context;
    ViewHolder viewHolder;
    ArrayList<Model_images> al_menu = new ArrayList<>();
    int int_position;


    public GridViewAdapter(Context context, ArrayList<Model_images> al_menu,int int_position) {
        super(context, R.layout.activity_adapter__photos_folder, al_menu);
        this.al_menu = al_menu;
        this.context = context;
        this.int_position = int_position;
    }

    @Override
    public int getCount() {

        Log.e("ADAPTER LIST SIZE", al_menu.get(int_position).getAl_imagepath().size() + "");
        return al_menu.get(int_position).getAl_imagepath().size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        if (al_menu.get(int_position).getAl_imagepath().size() > 0) {
            return al_menu.get(int_position).getAl_imagepath().size();
        } else {
            return 1;
        }
    }

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


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {

            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_adapter__photos_folder, parent, false);
            viewHolder.tv_foldern = (TextView) convertView.findViewById(R.id.tv_folder);
            viewHolder.tv_foldersize = (TextView) convertView.findViewById(R.id.tv_folder2);
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_foldern.setVisibility(View.GONE);
        viewHolder.tv_foldersize.setVisibility(View.GONE);



        Glide.with(context).load("file://" + al_menu.get(int_position).getAl_imagepath().get(position))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(viewHolder.iv_image);


        return convertView;

    }

    private static class ViewHolder {
        TextView tv_foldern, tv_foldersize;
        ImageView iv_image;

    }
}

LOGCAT:

10-20 17:27:02.769 24402-24402/com.example.dell_1.Myapp3 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.dell_1.Myapp3, PID: 24402
                                                                           java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                               at java.util.ArrayList.get(ArrayList.java:411)
                                                                               at com.example.dell_1.myapp3.ImageViewer.PhotosActivity$2$1$1.onClick(PhotosActivity.java:85)
                                                                               at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Selvin Oct 20 '17 at 12:09

1 Answers1

0

Based on your code, I don't see where the ArrayList al_menu is ever loaded with data. So when your onClick executes and attempts to get item at index 0, you get an IndexOutOfBoundsException exception. Looks like you for got to provide it with default values. Also inside of GridViewAdapter you the declare and instantiate al_menu at the top of the file and then overwrite it inside of the constructor. That is unnecessary and causing an extra object to be used and thrown away and doesn't protect against a NullPointerException later in the adapter because null could be passed into the GridViewAdapter' constructor and you don't check to make sure of that. I recommend adding a condition that checks if the passed in ArrayList is not null and add the values to your ArrayList using a for loop or even removed the instantiation line from the top of the file and do it in the GridViewAdapter's constructor and use in the ArrayList argument as a parameter if it isn't null as parameter when you instantiate your al_menu. Example:

public class GridViewAdapter extends ArrayAdapter<Model_images> {

Context context;
ViewHolder viewHolder;
ArrayList<Model_images> al_menu;
int int_position;


public GridViewAdapter(Context context, ArrayList<Model_images> al_menu,int int_position) {
    super(context, R.layout.activity_adapter__photos_folder, al_menu);
    if(al_menu != null) {
       this.al_menu = new ArrayList(al_menu);
    }
    this.context = context;
    this.int_position = int_position;
}

@Override
public int getCount() {

    Log.e("ADAPTER LIST SIZE", al_menu.get(int_position).getAl_imagepath().size() + "");
    return al_menu.get(int_position).getAl_imagepath().size();
}

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public int getViewTypeCount() {
    if (al_menu.get(int_position).getAl_imagepath().size() > 0) {
        return al_menu.get(int_position).getAl_imagepath().size();
    } else {
        return 1;
    }
}

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


@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_adapter__photos_folder, parent, false);
        viewHolder.tv_foldern = (TextView) convertView.findViewById(R.id.tv_folder);
        viewHolder.tv_foldersize = (TextView) convertView.findViewById(R.id.tv_folder2);
        viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);


        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.tv_foldern.setVisibility(View.GONE);
    viewHolder.tv_foldersize.setVisibility(View.GONE);



    Glide.with(context).load("file://" + al_menu.get(int_position).getAl_imagepath().get(position))
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .into(viewHolder.iv_image);


    return convertView;

}

private static class ViewHolder {
    TextView tv_foldern, tv_foldersize;
    ImageView iv_image;

}
}
cincy_anddeveloper
  • 1,140
  • 1
  • 9
  • 19