0

我正在尝试从列表视图中删除特定行,但我的应用程序不断崩溃。

首先,我尝试仅删除列表适配器中的项目,但它崩溃了:

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

然后我也尝试直接从 listView 中删除它,但它也崩溃了:

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

toDelete 是一个整数变量,它具有我单击的行的编号 id。

那么如何从列表视图中删除特定行呢?

这是完整的代码,如果有人想要的话:

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");
                }
            });

扩展“它崩溃”部分真的很难。很简单:只要我点击“是”按钮,应用程序就会崩溃。如果有帮助,这里是 logcat 文件:

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)
4

2 回答 2

0

我认为崩溃不是因为删除,而是因为更新。

关键是

listAdapter.notifyDataSetChanged();

你不能在里面做

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

由于它在 UIThread 之外(有人称它为 Main Thread),因此您只能在 UI Thread 内修改 UI。notifyDataSetChanged()打算修改UI。

要解决此问题,您可以改用处理程序。像这样做:

**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");
                }
            });
于 2013-08-06T17:12:00.367 回答
0

我发现了问题!这完全是我的错,我是一个肤浅的人。

问题是我在删除行之前调用了 dialog.dismiss() 。我只是在 onclick 的末尾移动了 dialog.dismiss() ,现在它可以正常工作了:)

这是现在的代码:

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();
                }
            });
于 2013-08-06T21:20:18.343 回答