繁体   English   中英

即使Android重新启动后也保存/获取列表

[英]Save/Fetch List even after Android reboot

我正在使用以下代码保存ArrayList并将其写入文件。 仅当应用程序已后台运行时,此方法才能维护列表(onStop(),我认为),但是当我重新启动手机时,列表会丢失:

@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();
}

编辑- 创建方法

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);

}

如您所见,我已重写OnStop()和OnDestroy方法来尝试执行此操作。 我已经将writeListToFile()放在OnCreate()方法中。 任何解决方案表示赞赏。

好吧,是否有可能在重新启动时将空数组列表写入文件并随后尝试访问该列表? 我感觉一切都在正确地编写和读取,但是存在逻辑缺陷,即破坏文本文件(当调用它时MODE_PRIVATE删除文件中的所有内容,而MODE_APPEND写入该文件的末尾)或向其写入空白arrayList。 我可以看看您的onCreate()方法吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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