0

I'm trying to remove a specific row from listview, but my app keeps crashing.

First, I tried removing item form listAdapter only, but it crashes:

listAdapter.remove(listAdapter.getItem(toDelete));
listAdapter.notifyDataSetChanged();

Then I also tried removing it from listView directly but it also crashes:

listView.removeViewAt(toDelete);
listAdapter.notifyDataSetChanged();

toDelete is an integer variable that has a number id of the row that I'm clicking.

So how could I delete a specific row from listview?

This is the full code, if anyone wants it:

listView.setOnItemLongClickListener(new OnItemLongClickListener(){

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            deleteAlert.setTitle("Warning");
            deleteAlert.setMessage("Are you sure you want to delete this?");
            toDelete = arg2;
            deleteAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    File directory = new File (externalStoragePath + File.separator + "Android/data/com.whizzappseasyvoicenotepad/");
                    File deleteFile = new File (directory, fileNames.get(toDelete) + ".mp3");
                    deleteFile.delete();
                    dialog.dismiss();

                    listView.removeViewAt(toDelete);
                    listAdapter.notifyDataSetChanged();
                    Log.i("TAG", "Deleting file: " + directory + fileNames.get(toDelete) + ".mp3");
                }
            });

It's really hard to expand the "it crashes" part. It's simple: as soon as I click the "yes" button, the app crashes. Here's the logcat file if it helps:

08-06 19:05:07.437: E/AndroidRuntime(16257): FATAL EXCEPTION: main
08-06 19:05:07.437: E/AndroidRuntime(16257): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
08-06 19:05:07.437: E/AndroidRuntime(16257):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at java.util.ArrayList.get(ArrayList.java:308)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at com.whizzappseasyvoicenotepad.RecordedLibrary$2$1.onClick(RecordedLibrary.java:113)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at android.os.Looper.loop(Looper.java:137)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at java.lang.reflect.Method.invokeNative(Native Method)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at java.lang.reflect.Method.invoke(Method.java:525)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-06 19:05:07.437: E/AndroidRuntime(16257):    at dalvik.system.NativeStart.main(Native Method)
Guy
  • 6,414
  • 19
  • 66
  • 136
  • Well the error is self-explanatory. Your arraylist has a size of 0. Have you tried any of the suggestions [here on SO](http://stackoverflow.com/a/5344958/645270)? – keyser Aug 06 '13 at 17:09
  • `listAdapter.remove(listAdapter.getItem(toDelete));` is the proper way. Can you post your adapter code please. – M-Wajeeh Aug 06 '13 at 17:10

2 Answers2

0

I think the crash is not because of removing, it's about updating.

The key thing is

listAdapter.notifyDataSetChanged();

You can't do it inside

public void onClick(DialogInterface dialog, int which)
{
}

Since it's outside the UIThread(some people call it Main Thread), you can only modify UI inside the UI Thread. notifyDataSetChanged() is gonna to modify the UI.

To solve this, you can use a Handler instead. Do it like this:

**Handler myHandler = new Handler()
{
   public void handleMessage(android.os.Message msg)
   {
      listAdapter.notifyDataSetChanged();
   }
};**
listView.setOnItemLongClickListener(new OnItemLongClickListener(){

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            deleteAlert.setTitle("Warning");
            deleteAlert.setMessage("Are you sure you want to delete this?");
            toDelete = arg2;
            deleteAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    File directory = new File (externalStoragePath + File.separator + "Android/data/com.whizzappseasyvoicenotepad/");
                    File deleteFile = new File (directory, fileNames.get(toDelete) + ".mp3");
                    deleteFile.delete();
                    dialog.dismiss();

                    listView.removeViewAt(toDelete);
                    **myHandler.sendMessage();**
                    Log.i("TAG", "Deleting file: " + directory + fileNames.get(toDelete) + ".mp3");
                }
            });
Scott Zhu
  • 8,341
  • 6
  • 31
  • 38
  • Thank you very much for taking your time and reply, but I found a simpler solution (check my answer) :) – Guy Aug 06 '13 at 21:22
0

I found the issue! It was completely my fault, I'm a shallow guy.

The problam was that I called dialog.dismiss() before deleting rows. I simply moved the dialog.dismiss() at the end of the onclick and now it works as it should :)

This is the code now:

deleteAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    File directory = new File (externalStoragePath + File.separator + "Android/data/com.whizzappseasyvoicenotepad/");
                    File deleteFile = new File (directory, fileNames.get(toDelete) + ".mp3");
                    deleteFile.delete();
                    Log.i("TAG", "Deleting file: " + directory + fileNames.get(toDelete) + ".mp3");

                    listAdapter.remove(listAdapter.getItem(toDelete));
                    listAdapter.notifyDataSetChanged();

                    dialog.dismiss();
                }
            });
Guy
  • 6,414
  • 19
  • 66
  • 136