简体   繁体   中英

Save/Fetch List even after Android reboot

I am saving and writing an ArrayList using the following code to a file. This method works to sustain the list only when the application is backgrounded(onStop(), i presume), but when I reboot the phone, my list is lost:

@SuppressWarnings("unchecked")
private ArrayList<HashMap<String, String>> loadListFromFile(
        ArrayList<HashMap<String, String>> masterlistrev) {

    try {
        FileInputStream fis = openFileInput(serfilename);
        ObjectInputStream ois = new ObjectInputStream(fis);
        masterlistrev = (ArrayList<HashMap<String, String>>) ois
                .readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return masterlistrev;
}

private void writeListToFile(
        ArrayList<HashMap<String, String>> masterlistrev, Context ctx,
        String filename) {

    FileOutputStream fos;
    try {
        fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(masterlistrev);
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

protected void onStop() {
    super.onStop();
    writeListToFile((ArrayList<HashMap<String, String>>) painItems,
            getApplicationContext(), serfilename);
}
protected void onDestroy(){

    writeListToFile((ArrayList<HashMap<String, String>>) painItems,
            getApplicationContext(), serfilename);
    super.onDestroy();
}

EDIT-- ON CREATE METHOD

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.addscreen);
    // getPainItems from the saved file
    if (loadListFromFile((ArrayList<HashMap<String, String>>) painItems) != null)
        painItems = loadListFromFile((ArrayList<HashMap<String, String>>) painItems);
    // if it's the first time opening the app, then go to 'AddMyInfo.class'
    serfilename = "hello";
    SharedPreferences settings = this.getSharedPreferences("MyApp", 0);
    SharedPreferences.Editor e = settings.edit();
    boolean firstrun = settings.getBoolean("firstrun", true);
    if (firstrun) {
        e.putBoolean("firstrun", false);
        e.commit();
        Intent intent = new Intent(this, AddMyInfo.class);
        startActivity(intent);
    }
    // initialize painTitle and set its font
    painTitle = (TextView) findViewById(R.id.painTitle);
    Typeface font = Typeface.createFromAsset(getAssets(),
            "Chantelli_Antiqua.ttf");
    painTitle.setTypeface(font);
    listthings = (ListView) findViewById(R.id.listthings);
    from = new String[] { "row_1", "row_2" };
    to = new int[] { R.id.row1, R.id.row2 };

    adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
            from, to);
    listthings.setAdapter(adapter);
    listthings.setOnItemClickListener(this);
    listthings.setOnItemLongClickListener(this);

    addButton = (Button) findViewById(R.id.addButton);
    addButton.setOnClickListener(this);
    listthings.setChoiceMode(CHOICE_MODE_SINGLE);

}

As you can see, I have overriden the OnStop() and OnDestroy methods to try to do this. I have put writeListToFile() in the OnCreate() Method. Any solution appreciated.

Well, is it possible upon relaunch you are writing your empty array list to your file and subsequently trying to access said list? I have a feeling everything is writing and reading correctly but there is a logical flaw either destroying your text file (MODE_PRIVATE erases everything in a file when you call it, while MODE_APPEND writes to the end of it) or writing a blank arrayList to it. Could I see your onCreate() Method?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM