如何使用对象数组

时间:2016-10-09 18:25:36

标签: java listview

大家好我是java的新手,我被卡住了。

我创建了一个名为song的类,其代码如下

package musiclibrary;

public class Song {
    private String title;
private String albumArtist;
private String genre;
private double price;
private int duration;

    public Song() {
    }

        public Song(String title, String albumArtist, String genre, double price, int duration) {
        this.title = title;
        this.albumArtist = albumArtist;
        this.genre = genre;
        this.price = price;
        this.duration = duration;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAlbumArtist() {
        return albumArtist;
    }

    public void setAlbumArtist(String albumArtist) {
        this.albumArtist = albumArtist;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int durationSeconds) {
        this.duration = durationSeconds;
    }
}

我创建了一个对象数组,如下所示

     private  ObservableList<Song> songs =FXCollections.observableArrayList();
     public  ObservableList<Song> load()
     {
     songs.add(new Song("J. Cole", "Lost Ones", "Hip Hop", 24.66, 3000));
     songs.add(new Song("Erykah Badu", "Time's a wasting", "Neo-Soul", 24.66, 4000));
     songs.add(new Song("Common", "Book of Life", "Hip Hop", 24.66, 2000));
     songs.add(new Song("CommonGround", "dasdsad", "asdsadsad", 24.66, 3000));
     return  songs;
     }

问题是我不知道怎么称呼#34;加载&#34;函数在mainView方法的listView中。以下是我所拥有的

  private ListView<Song> ListView;
    @Override
    public void initialize(URL url, ResourceBundle rb) {

          PlayList pl= new PlayList();
          ListView.setItems(pl.play());



    }

附上是结果。请帮忙!

Result

1 个答案:

答案 0 :(得分:1)

好像您正在显示从java.lang.Object继承的默认toString()。您应该在Song中覆盖它并显示您想要的信息。例如,我们假设您只想显示歌曲的标题和艺术家:

@Override
public Sting toString() {
    return title + " by " + albumArtist;
}