我有一个带有 longclicklistener 的文件列表,它会显示一个带有删除和重命名选项的上下文菜单。它们启动 deleteDialog() 或 renameDialog()。它们调用 delete() 或 rename()。删除有效,但重命名给出:
05-05 10:26:44.105: W/System.err(19017884): java.io.FileNotFoundException: Failed to rename file: /sdcard/My Webs/new/index.php
甚至认为我可以在这个位置的文件系统上看到这个文件。
这是我的警报代码:
void delete(File f) throws IOException {
if (f.isDirectory()) {
for (File c : f.listFiles())
delete(c);
}
if (!f.delete())
throw new FileNotFoundException("Failed to delete file: " + f);
}
void rename(File f, String newName) throws IOException {
File newFile = new File(newName);
f.renameTo(newFile);
if (!f.renameTo(newFile))
throw new FileNotFoundException("Failed to rename file: " + f);
}
public void delDialog(int position) {
final int pos = position;
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.remove);
alertDialog.setTitle(getString(R.string.delete));
alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String selectedFileString = directoryEntries.get(pos).getText();
File tmpFile = new File(currentDirectory.toString()
+ selectedFileString);
try {
delete(tmpFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
directoryEntries.remove(pos);
itla.notifyDataSetChanged();
currentFile = null;
changed = false;
return;
}
});
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.setMessage("Are you sure you want to delete this file?");
alertDialog.show();
}
public void renameDialog(int position) {
final int pos = position;
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.renameicon);
alertDialog.setTitle(getString(R.string.rename));
alertDialog.setButton("Rename", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String selectedFileString = directoryEntries.get(pos).getText();
File tmpFile = new File(currentDirectory.toString()
+ selectedFileString);
try {
rename(tmpFile, "test.html");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
itla.notifyDataSetChanged();
return;
}
});
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.setMessage("Are you sure you want to rename this file?");
alertDialog.show();
}
public void Show_Context(Context context, String message, int position) {
final AlertDialog customDialog = new AlertDialog.Builder(this).create();
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.contextmenu, null);
final Button del = (Button) view.findViewById(R.id.delBtn);
final Button rename = (Button) view.findViewById(R.id.renameBtn);
final int pos = position;
customDialog.setView(del);
customDialog.setView(rename);
del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
customDialog.dismiss();
delDialog(pos);
}
});
rename.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
customDialog.dismiss();
renameDialog(pos);
}
});
customDialog.setView(view);
customDialog.show();
}
如您所见,deleteDialog() 和 renameDialog() 的代码相同,但 renameDialog() 抛出 FileNotFoundException