我有一个文件列表,当单击文件时,其内容将显示在EditText中。该文件设置为currentFile。如果用户在保存旧文件之前尝试打开新文件,则会显示一个保存对话框。对话框上的“确定”按钮应保存当前工作文件,而是将其保存为用户尝试打开的文件。我的代码中的问题在哪里导致当前文件被保存在试图打开的新文件上。
public boolean exists;
public File currentFile;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
et = (EditTextLineNumbers) findViewById(R.id.ide);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
File dir = new File(Environment.getExternalStorageDirectory() + "/My Webs");
currentDirectory = dir;
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
changed = false;
}
@Override
public void afterTextChanged(Editable s) {
changed=true;
}
});
changed=false;
if(dir.isDirectory()) {
browseToRoot();
}else{
dir.mkdir();
}
}
private void openFile(File aFile){
String nullChk = et.getText().toString();
exists = true;
currentFile = aFile;
if(!changed || nullChk.matches("")){
try {
et.setText(new Scanner(aFile).useDelimiter("\\Z").next());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Save first?");
alert.setMessage("(Will be saved in the current working directory)");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String temptxt = et.getText().toString();
if(exists){
saveFile(currentFile.getPath(), temptxt);
}else{
saveAs();
}
}
});
final File tempFile = aFile;
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try {
et.setText(new Scanner(tempFile).useDelimiter("\\Z").next());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
changed=false;
}
});
alert.show();
}
}
private void saveFile(String sFileName, String sBody){
//Toast.makeText(this, exists +"", Toast.LENGTH_SHORT).show();
if (exists) {
try {
File tempfile = new File(sFileName);
FileWriter writer = new FileWriter(tempfile);
writer.write(sBody);
writer.flush();
writer.close();
changed=false;
Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
return;
} catch (IOException e) {
e.printStackTrace();
}
}else{
Toast.makeText(this, "Save as", Toast.LENGTH_SHORT).show();
saveAs();
}
}
private void saveAs(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Save as");
alert.setMessage("(Will be saved in the current working directory)");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
String tmpText = et.getText().toString();
try {
File tempfile = new File(currentDirectory, value);
FileWriter writer = new FileWriter(tempfile);
writer.write(tmpText);
writer.flush();
writer.close();
changed=false;
//itla.notifyDataSetChanged();
fill(currentDirectory.listFiles());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
/**
* This function browses up one level
* according to the field: currentDirectory
*/
private void upOneLevel(){
if(this.currentDirectory.getParent() != null && !this.currentDirectory.getPath().equals("/sdcard/My Webs")){
this.browseTo(this.currentDirectory.getParentFile());
}else{
//Do nothing
}
}
private void browseTo(final File aDirectory){
// On relative we display the full path in the title.
if(this.displayMode == DISPLAYMODE.RELATIVE)
this.setTitle(aDirectory.getAbsolutePath() + " :: " +
getString(R.string.app_name));
if (aDirectory.isDirectory()){
this.currentDirectory = aDirectory;
fill(aDirectory.listFiles());
}else{
openFile(aDirectory);
}
changed=false;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String selectedFileString = this.directoryEntries.get(position)
.getText();
if (selectedFileString.equals(getString(R.string.current_dir))) {
// Refresh
this.browseTo(this.currentDirectory);
} else if (selectedFileString.equals(getString(R.string.up_one_level))) {
this.upOneLevel();
} else {
File clickedFile = null;
switch (this.displayMode) {
case RELATIVE:
clickedFile = new File(this.currentDirectory
.getAbsolutePath()
+ this.directoryEntries.get(position)
.getText());
break;
case ABSOLUTE:
clickedFile = new File(this.directoryEntries.get(
position).getText());
break;
}
if (clickedFile != null)
currentFile=clickedFile;
this.browseTo(clickedFile);
}
}
}
答案 0 :(得分:2)
openFile()
的第三行:在有条件地询问用户是否要在打开新文件之前保存currentFile
之前,您更改currentFile
。当您实际打开另一个文件而不是之前更改currentFile
时,即使不小心也不会出现此问题。