戻るボタンを押すたびにアプリが呼び出さonSaveInstanceState()
れず、データを保存できません。スケジューラーアプリを作ろうとしていますが、戻るボタンを押しても設定済みのスケジュールを保存する必要があります。ソース ファイルから新しいデータを stringArray に追加して、ListView を動的に編集したいと考えています。私が抱えている問題は、ファイルの schedules.txt が保存されていないことです。プログラムが新しいアクティビティを開くたびに、ファイルは空白になります。
これは私がこれまでに持っているコードです:
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();
}