给它一个空的ArrayList时,JavaFX Listview不更新

时间:2018-11-17 16:44:01

标签: java listview javafx

我正在以MVC格式编写一个项目,并且应该成为音乐播放器。

在我的视图中,我具有Listview的库和播放列表,如下所示:

private ListView<ISong> library = new ListView<ISong>();
private ListView<ISong> playlist = new ListView<ISong>();

public ListView<ISong> getLibrary() {return library;}
public ListView<ISong> getPlaylist() {return playlist;}

public View() {
    HBox hBoxCenter = new HBox(library, playlist);

我们还有类“ Playlist”和“ Song”,它们都实现了给我们的接口,“ Playlist”具有以下ArrayList,开始时完全为空:

package model;

import interfaces.IPlaylist;
import interfaces.ISong;
import javafx.collections.ModifiableObservableListBase;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;

public class Playlist extends ModifiableObservableListBase<ISong> implements IPlaylist {
    private ArrayList<ISong> songs = new ArrayList<>();

    @Override
    public boolean addSong(ISong s) throws RemoteException {
        if (songs.add(s)){
            return true;
        }
        throw new RemoteException("Song could not be added!");
    }

    @Override
    public boolean deleteSong(ISong s) throws RemoteException {
        if (songs.contains(s)){
            songs.remove(s);
            return true;
            }
        throw new RemoteException("Song does not exist in this Playlist!");
    }

    @Override
    public boolean deleteSongByID(long id) throws RemoteException {
        for (int i = 0; i < songs.size(); i ++){
            if (songs.get(i).getId() == id){
                songs.remove(i);
                return true;
            }
        }
        throw new RemoteException("No Song with matching ID exists in Playlist!");
    }

    @Override
    public void setList(ArrayList<ISong> s) throws RemoteException {
        if (s != null) {
            songs = s;
            return;
        }
        throw new RemoteException("Playlist can not be null.");
    }

    @Override
    public ArrayList<ISong> getList() throws RemoteException {
        try {
            return songs;
        }catch(Exception e){throw new RemoteException();}
    }


    @Override
    public void clearPlaylist() throws RemoteException {
        try {
            songs.clear();
        }catch(Exception e){throw new RemoteException();}
    }

    @Override
    public int sizeOfPlaylist() throws RemoteException {
        try {
            return songs.size();
        }catch(Exception e){throw new RemoteException();}
    }


    @Override
    public ISong findSongByPath(String name) throws RemoteException {
        for(int i=0; i<songs.size();i++){
            ISong song = songs.get(i);
            if (song.getPath().equals(name)) return song;
        }
        throw new RemoteException("Could not find song with given Path!");
    }

    @Override
    public ISong findSongByID(long id) throws RemoteException {
        for(int i=0; i<songs.size();i++){
            ISong song = songs.get(i);
            if (song.getId()==id) return song;
        }
        throw new RemoteException("Could not find song with given ID!");
    }

    @Override
    public Iterator<ISong> iterator() {
        return null;
    }

    @Override
    public ISong get(int index) {
        return songs.get(index);
    }

    @Override
    public int size() {
        return songs.size();
    }

    @Override
    protected void doAdd(int index, ISong element) {
        songs.add(index, element);
    }

    @Override
    protected ISong doSet(int index, ISong element) {
        return null;
    }

    @Override
    protected ISong doRemove(int index) {
        return null;
    }
}

在我的控制器中,我这样链接它们:

public void link (Model model, View view){
    this.view = view;
    this.model = model;
    view.getLibrary().setItems(model.getLibrary());
    view.getPlaylist().setItems(model.getPlaylist());
}

MainClass如下所示:

public void start(Stage primaryStage) throws Exception {

    Model model = new Model();
    View view = new View();
    Controller controller = new Controller();

    controller.link(model,view);
    view.setController(controller);
    controller.initializePlaylist();

    Scene scene = new Scene(view);
    primaryStage.setScene(scene);
    primaryStage.show();
    controller.cleanup();

}

现在是我的问题了: 当GUI打开并且ArrayList为空时,它不会显示对ArrayList所做的任何进一步更改。但是,如果我在ArrayList中添加“空”歌曲,然后在打开GUI后简单地清除列表,则一切正常(这正是我编写initializePlaylist和cleanup方法的原因)。

很抱歉,如果这是一个愚蠢的问题,或者如果我没有提供足够的代码段,那么我现在真的没有主意,并且已经花了好几个小时来寻找原因。

0 个答案:

没有答案