开发者

Non-Functional writing and getting ArraryList Android

开发者 https://www.devze.com 2023-03-23 17:24 出处:网络
I use the following code to load and write list to a file. This was working when I closed the application(backgrounded it) and restarted it, but that stopped working now, and I do not recall changing

I use the following code to load and write list to a file. This was working when I closed the application(backgrounded it) and restarted it, but that stopped working now, and I do not recall changing a thing. Furthermore, the list is definitely not saving if I reboot the phone.

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()开发者_JS百科, serfilename);
}


It appears as if you're not closing your file correctly. Place the Close() method call of your streams in a finally block. Close your ObjectInputStream aswell.

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


You should set the filename before you try to load the data in the 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);

    ....


Here my test class:

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

精彩评论

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

关注公众号