每当我按下后退按钮,我的应用程序不会调用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