Today I started my first Android App-Project and - of course - I'm stuck with a problem. I want to make a really simple app do get into the the whole programming thing, and I'm in my holidays right now so I thought it would be interesting to save the times when I go to bed/when I wake up. So it shouldnt be so hard and nearly everything works like I want it to. Now the problem is, that I save my dates in an ArrayList, so they show up in a ListView. I read that I can't save ArrayLists in SharedPrefs, I didn't understand the SQLite thing, so I decided to use the FileOutputStream etc. I read about it on the internet and tried to do everything like they did but it simply doesnt work. There isn't an error in the console but if I close and reopen the app, all my elements are gone. Anyone who can help me? :) (I dont know which part of the code is important and which isn't, so here's the whole code)
package com.gaminggears.sleeplist.sleep;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Path;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class MainActivity extends AppCompatActivity{
private ListView dataListView;
private ListView dataListView1;
private ArrayAdapter arrayAdapter;
File file = new File("/mnt/sdcard/SleepApp/save.sav");
File dir = new File("/mnt/sdcard/SleepApp");
Date date = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("E d-M-y kk:mm");
ArrayList<String> data = new ArrayList<String>();
ArrayList<String> data1 = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!dir.exists()){
file.mkdir();
}
loadDataFromFile();
dataListView1 = (ListView) findViewById(R.id.listView2);
dataListView = (ListView) findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
dataListView.setAdapter(arrayAdapter);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data1);
dataListView1.setAdapter(arrayAdapter);
System.out.println("\t\t" + data);
}
public void sleepClick(View v) {
data.add(ft.format(date));
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
dataListView.setAdapter(arrayAdapter);
saveDataToFile();
}
public void wakeUpClick(View v){
data1.add(ft.format(date));
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data1);
dataListView1.setAdapter(arrayAdapter);
saveDataToFile();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void saveDataToFile(){
try{
FileOutputStream sF = new FileOutputStream(file);
ObjectOutputStream save = new ObjectOutputStream(sF);
save.writeObject(data);
save.writeObject(data1);
save.close();
} catch(Exception exc){
exc.printStackTrace();
}
}
public void loadDataFromFile(){
try{
FileInputStream saveFile = new FileInputStream(file);
ObjectInputStream save = new ObjectInputStream(saveFile);
data.clear();
data.addAll((ArrayList) save.readObject());
data1.clear();
data1.addAll((ArrayList) save.readObject());
save.close();
} catch(Exception exc) {
exc.printStackTrace();
}
}
}
EDIT: updated the code. Now my Error is "java.io.FileNotFoundException: /mnt/sdcard/SleepApp/save.sav: open failed: EACCES (Permission denied)". Any ideas?