title: JavaFX, List to ObservableList to ListView
body: <p>So my problem is, that I have an serilized ArrayList and have to update it in my GUI to show its content in a ListView dynamically.
  The serialization and deserialization works fine with the use of a DAO interface but the GUI won't refresh my ListView.</p>
  
  <p>This class holds my data interaction (mostly save, load...):</p>
  
  <pre><code>public class Medienverwaltung implements Serializable, IDAO{
  
      private static final long serialVersionUID = 1L;
      private List&lt;Medium&gt; medienliste;
      public ObservableList&lt;Medium&gt; obList;   //public for test-reasons
  
      public Medienverwaltung(){
          medienliste = new ArrayList&lt;Medium&gt;();
          obList = FXCollections.observableArrayList(medienliste);
      }
  
      //[...]
  
      public List&lt;Medium&gt; getMedienliste(){
          return this.medienliste;
      }
      //[...]
  }
  </code></pre>
  
  <p>Here comes my GUI implementation snippet:</p>
  
  <pre><code>public class HauptFenster extends Application{
  
      private Medienverwaltung medienverwaltung;
  
      @Override
      public void start(Stage stage) throws Exception{
          medienverwaltung = new Medienverwaltung();
  
          VBox root = new VBox();
          ListView&lt;String&gt; showliste = new ListView&lt;String&gt;();
          MenuBar menuBar = createMenuBar(stage);
          root.getChildren().add(menuBar);
          root.getChildren().add(showliste);
  
          //Make Listener and refresh the shown list!
          medienverwaltung.obList.addListener(new ListChangeListener&lt;Medium&gt;(){
              @Override
              public void onChanged(ListChangeListener.Change&lt;? extends Medium&gt; change) {
                  showliste.getItems().clear();
                  for(Medium medium : medienverwaltung.obList){
                      //toString() is overwritten and works, too
                      showliste.getItems().add(medium.toString());
                  }
              }
          });
          // this adds a Medium object to the Arraylist in Medienverwaltung
          medienverwaltung.aufnehmen(new Bild("Foto12", 2017, "Zuhause"));
  
          stage.setTitle("Medien Verwaltung");
          stage.setScene(new Scene(root, 800, 400) );
          stage.show();   
      }
      //[...]
  </code></pre>
  
  <p>I also tired to exchange the whole ArrayList from the class "Medienverwaltung" with an ObservableList, so that there is only one List remaining, which works for the GUI but not for the serialization and deserialization as I guessed before. (and tried a few other implementations)</p>
  
  <p>Does anyone have an idea how to change my code so that it works?
  And my second question is, what is the best way in terms of a 3 layer architecture?</p>
  
  <p><em>The following is a reference to Fabians Answer and responds to my comment on that</em></p>
  
  <p><strong>Update#1.1</strong>  (addendum for explanation)</p>
  
  <pre><code>public interface IDAO {
      // Save method
      void speichern(List&lt;Medium&gt; liste) throws PersistenzException;
      // Load method
      List&lt;Medium&gt; laden() throws PersistenzException;
  }
  </code></pre>
  
  <p><em>Here comes my concrete save Method:</em></p>
  
  <pre><code>@Override
  public void speichern(List&lt;Medium&gt; medienliste) throws PersistenzException{
      File sfile = new File("medienliste.dat");
  
      try(FileOutputStream fos = new FileOutputStream(sfile); ObjectOutputStream oos = new ObjectOutputStream(fos)){
          oos.writeObject(medienliste);
          System.out.println("Serialisierung erfolgreich!");
      }catch(IOException e){
          e.printStackTrace();
          System.out.println("Serialisierung fehlgeschlagen!");
      }
  }
  </code></pre>
  
  <p><strong>Update#1.2</strong> (addendum for explanation)</p>
  
  <pre><code>//[...]  section of my GUI for saving
  MenuItem speichern = new MenuItem("Speichern");
      speichern.setOnAction(new EventHandler&lt;ActionEvent&gt;(){
          @Override
          public void handle(ActionEvent e){
              try{
          //Before:    medienverwaltung.speichern(medienverwaltung.getMedienliste()); -&gt; doesn't work because of serializing an ObservableList
                  medienverwaltung.speichern(medienverwaltung.getBackingList());
              }catch(PersistenzException pe){
                  pe.printStackTrace();
              }
          }
      });
  //[...]
  </code></pre>
  
  <p>But as I guess, it's not a fine way to access the backinlist this way.</p>
  
  <p><strong>Update#2:</strong></p>
  
  <p>to respect the principle of encapsulation in a clean way I now added an overloaded Method in the class Medienverwaltung:</p>
  
  <pre><code>public void speichern() throws PersistenzException{
      speichern(backingList);
  }
  </code></pre>
  
  <p>So my GUI now only calls speichern(). This actually calls the method for saving with the backedlist which is no more accessible from the outside. I hope this is no bad coding style ^^</p>
  
  <p>BTW.: If you are reading this and have a similar problem, don't use <strong>ObservableArrayList</strong> for the synchronisation with a normal <strong>List</strong>, this won't work! Use <strong>ObservableList</strong> instead.</p>