我试图从一个简单的ArrayList的内容填充ListView.这是我的
Controller.java文件:
package design;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
public class Controller {
@FXML private Button buttontest;
@FXML private ListView<Song> listViewofSongs;
protected List<Song> songList = new ArrayList<>();
protected ListProperty<Song> listProperty = new SimpleListProperty<Song>();
Song Language = new Song("Peter", "myalbum", "yes", 2010);
public void addSong(Song song){
songList.add(song);
}
public void initialize(URL url, ResourceBundle rb) {
addSong(Language);
listViewofSongs.setItems(FXCollections.observableList(songList));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Style.fxml文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个功能,我需要列表视图中当前所选项目的索引号.因此,如果它在列表中排在第4位,我需要一个返回3的方法,因为我需要编辑ArrayList中的相应对象.
我怎样才能做到这一点?