我正在尝试从列表视图中删除特定行,但我的应用仍在崩溃。
首先,我尝试仅删除项目表单listAdapter,但它崩溃了:
listAdapter.remove(listAdapter.getItem(toDelete));
listAdapter.notifyDataSetChanged();
然后我也尝试直接从listView中删除它,但它也崩溃了:
listView.removeViewAt(toDelete);
listAdapter.notifyDataSetChanged();
toDelete是一个整数变量,其中包含我要点击的行的数字ID。
那我怎么能从listview中删除一个特定的行?
这是完整的代码,如果有人想要的话:
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)
答案 0 :(得分:0)
我认为崩溃不是因为删除,而是因为更新。
关键是
listAdapter.notifyDataSetChanged();
你不能在
内完成public void onClick(DialogInterface dialog, int which)
{
}
因为它在UIThread之外(有些人称之为主线程),所以你只能修改UI线程内的UI。 notifyDataSetChanged()
将修改用户界面。
要解决此问题,您可以使用Handler。 这样做:
**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");
}
});
答案 1 :(得分: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();
}
});