0

I have an activity which contains a listview, the activity (MainActivity) launches another activity (HomeworkAddActivity), this retrieves a string from the user and adds it to the listview (all of the above works).

However in order that the listview 'remembers' its contents whilest the other activity is launched I try to save the list to a file, code:

public void saveHomework() {
    try {
        @SuppressWarnings("resource")
        FileOutputStream fos = new FileOutputStream(homeworkFileName);
        fos = openFileOutput(homeworkFileName,
                Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(homeworkItems);
        os.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@SuppressWarnings("unchecked")
public void populateHomework() {
    try {
        @SuppressWarnings("resource")
        FileInputStream fis = new FileInputStream(homeworkFileName);
        fis = getParent().openFileInput(homeworkFileName);
        ObjectInputStream is = new ObjectInputStream(fis);
        homeworkItems = (ArrayList<String>) is.readObject();
        is.close();
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

I then read it from the file.

saveHomework is called onPause

and populateHomework:

    @Override
public void onResume() {
    super.onResume();
    homeworkItems = new ArrayList<String>();
    activity = this;
    homeworkListAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, homeworkItems);
    populateHomework();
    homeworkListAdapter.notifyDataSetChanged();
}

However it only ever shows the items added by addHomeworkActivity, not those 'saved' and 'restored' (they don't get saved/restored successfully), why is this?

ollie299792458
  • 222
  • 3
  • 16
  • Make sure you have checked that homeworkFile exists or not! and also make sure of permissions if using external storage! everything else seems to be okay! – ashu Apr 13 '14 at 18:45
  • I didn't get your question, you have list (suppose call list A) and then you save while onPause, and then you restore while onResume()... and then what ? – Sruit A.Suk Apr 13 '14 at 18:55
  • @ashu i am using internal storage and i was under the impression that opening the stream populated the file. – ollie299792458 Apr 13 '14 at 19:54
  • @toonsuperlove the key point is that the save and restore dont work, i will/have updated the question. Also after onResume the list is put in the listview – ollie299792458 Apr 13 '14 at 19:55

1 Answers1

0

It has 2 problems

  1. your listview still connect with your old adapter

  2. notifyDataSetChanged might not work in your case, it relate to this answer

notifyDataSetChanged example

you need to reorder your command in onResume()

@Override
public void onResume() {
    super.onResume();
    homeworkItems = new ArrayList<String>();
    activity = this;
    populateHomework(); // I move this line up
    homeworkListAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, homeworkItems);

    //homeworkListAdapter.notifyDataSetChanged(); // this line might not work
    yourListView.setAdapter(homeworkListAdapter); // because your listview still connect with your old ArrayAdapter

}
Community
  • 1
  • 1
Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71