非功能性编写和获取ArrayList Android

时间:2011-07-27 05:56:35

标签: java android file listview

我使用以下代码加载列表并将其写入文件。当我关闭应用程序(背景它)并重新启动它时,这工作正常,但现在停止工作了,我不记得改变了什么。此外,如果我重新启动手机,列表肯定不会保存。

List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();

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

    serfilename = "hello";

....


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(){
    super.onDestroy();
    writeListToFile((ArrayList<HashMap<String, String>>) painItems,
            getApplicationContext(), serfilename);
}

3 个答案:

答案 0 :(得分:0)

好像您没有正确关闭文件。将您的流的Close()方法调用放在finally块中。关闭你的ObjectInputStream以及。

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

答案 1 :(得分:0)

在尝试加载onCreate(...)

中的数据之前,应设置文件名
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.addscreen);

    serfilename = "hello";

    // getPainItems from the saved file
    if (loadListFromFile((ArrayList<HashMap<String, String>>) painItems) != null)
        painItems = loadListFromFile((ArrayList<HashMap<String, String>>) painItems);

    ....

答案 2 :(得分:0)

这是我的测试类:

private List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
private static final String serfilename = "hello";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addscreen);

    // getPainItems from the saved file
    loadListFromFile();

    // here are my tests:
    // at the first run a hash map will be added and with the onStop saved
    // at the next runs the data will be read and logged
    if (painItems.size() == 0) {
        painItems = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("a", "b");
        painItems.add(map);
    } else {
        Log.d("test", "Key a: " + (String)(painItems.get(0).get("a")));
    }
}

private void loadListFromFile() {
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    try {
        fis = openFileInput(serfilename);
        ois = new ObjectInputStream(fis);
        painItems = (ArrayList<HashMap<String, String>>)ois.readObject();
    } catch (Exception e) {}
    try {
        ois.close();
    } catch (Exception e) {}
    try {
        fis.close();
    } catch (Exception e) {}
}

private void writeListToFile() {
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = getApplicationContext().openFileOutput(serfilename, MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(painItems);
        oos.close();
    } catch (Exception e) {}
    try {
        oos.close();
    } catch (Exception e) {}
    try {
        fos.close();
    } catch (Exception e) {}
}

protected void onStop() {
    super.onStop();
    writeListToFile();
}

protected void onDestroy() {
    super.onDestroy();
    writeListToFile();
}