每當我按下後退按鈕,我的應用程序不會調用onSaveInstanceState()
,我無法保存數據。我試圖製作一個調度程序應用程序,我需要保存已經設置的時間表,即使在我按下後退按鈕後也是如此。我想通過從源文件向stringArray添加新數據來動態編輯ListView。我遇到的問題是文件schedules.txt沒有保存。每當程序打開一個新的活動,該文件現在是空白的。如何永久保存數據,以便以後在Android中訪問它?
這是我的代碼至今:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedules);
ListView listview = (ListView) findViewById(R.id.scheduleListView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, schedulesList);
listview.setAdapter(adapter);
Log.v("myapp", "currentlist of files associated with this program are: " + fileList());
try {
FileOutputStream fout = openFileOutput("schedules.txt", MODE_PRIVATE);
Log.v("myapp", "FileOutputStream ran");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Log.v("myapp", "first get old schedules call");
getOldSchedules();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public void editFile(String format) throws IOException {
Log.v("myapp", "editFile ran");
FileOutputStream fout = openFileOutput("schedules.txt", MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fout);
writer.write("hello alex");
writer.flush();
writer.close();
Log.v("myapp", "secondary getoldschedules call");
getOldSchedules();
}
public void getOldSchedules() throws IOException{
FileInputStream fis = openFileInput("schedules.txt");
InputStreamReader reader = new InputStreamReader(fis);
char[] inputbuffer = new char[32];
reader.read(inputbuffer);
String data = new String(inputbuffer);
Log.v("myapp", "data in file reads: " + data);
reader.close();
}
您可能有興趣使用SQLite。不錯的教程:[SQLite數據庫和ContentProvider](http://www.vogella.com/articles/AndroidSQLite/article.html) – nkr