好吧,我知道这是一个非常奇怪的问题,但这是一个非常奇怪的问题。
我做的是一个笔记应用,基本上用户可以创建笔记本,然后在每个笔记本上做笔记。我遇到的问题是删除上述注释。
Activity 层次结构:书籍(主要 Activity )-> Book -> Note
基本上,当您删除笔记(从 Note Activity 完成)时,Note Activity 结束,Book 调用 onResume() 查看笔记本文件夹,并将所有笔记重新加载到 ListView 中。奇怪的是,当 Note Activity 结束时,您在 Book Activity 中看不到笔记。但是,如果您重新加载它(转到 Books 然后返回),或执行任何操作来调用刷新方法,便笺会再次出现并包含所有数据。
这是我用来删除笔记的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Are You Sure?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File note = new File(Path);
if(note.delete()) {
finish();
} else {
Snackbar.make(edt_note, "An Error Occurred", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
}
});
builder.setNegativeButton("Cancel", null).create();
builder.show();
note.delete() 在运行时返回 true。
作为旁注,我有权删除这些本地文件夹。删除笔记本的代码运行起来很神奇:
final AppCompatActivity This = this;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Are You Sure?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File folder = new File(getFilesDir().getPath() + File.separator + Name);
File[] notes = folder.listFiles();
for(File note : notes) {
note.delete();
}
if(folder.delete()) {
finish();
} else {
Snackbar.make((ListView) This.findViewById(R.id.list_notes), "An Error Occurred", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
}
});
builder.setNegativeButton("Cancel", null).create();
builder.show();
我真的被这个问题难住了,任何帮助都将不胜感激。
编辑:
它不会让我自己选择已解决的答案,但我想通了。详情如下。
最佳答案
哇,我傻了!哈哈
发生的事情是我在 onStop() 中有一个保存方法,它在 Activity 结束时保存笔记。哎呀!
关于java - Android Java File.delete() 删除文件,然后它自己取消删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34540439/