简体   繁体   English

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

[英]Save/Fetch List even after Android reboot

I am saving and writing an ArrayList using the following code to a file. 我正在使用以下代码保存ArrayList并将其写入文件。 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: 仅当应用程序已后台运行时,此方法才能维护列表(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();
}

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. 如您所见,我已重写OnStop()和OnDestroy方法来尝试执行此操作。 I have put writeListToFile() in the OnCreate() Method. 我已经将writeListToFile()放在OnCreate()方法中。 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. 我感觉一切都在正确地编写和读取,但是存在逻辑缺陷,即破坏文本文件(当调用它时MODE_PRIVATE删除文件中的所有内容,而MODE_APPEND写入该文件的末尾)或向其写入空白arrayList。 Could I see your onCreate() Method? 我可以看看您的onCreate()方法吗?

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

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