开发者

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>>) o开发者_运维百科is
                .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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜