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)