向listview中的每个元素添加listview?

liwlm1x9  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(365)

我正在尝试将一个listview附加到另一个listview中的每个元素,并且该listview连接到sql数据库。因此,在listview中选择一个值,然后弹出另一个listview。但我不知道如何把它们和听众联系起来。这是我到目前为止所做的,希望你能理解我的问题。应该是这样的

public static void main(String[] args) {
    launch(args);
}
private ArrayList<String> getArtister () {
    ArrayList<String> artister = new ArrayList<>();
    try (Connection conn = DriverManager.getConnection(
            "jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
            "1ik013v20-12", "aubergine")) {
        Statement statement = conn.createStatement();
        ResultSet result = statement.executeQuery("SELECT Namn FROM artist");
        while (result.next()) {
            artister.add(result.getString(1));
        }
    } catch (SQLException ex) {
        System.out.println("Something went wrong...");
    }
    return artister;
}
private ArrayList<String> getLåtar () {
    ArrayList<String> låtar = new ArrayList<>();
    try (Connection conn = DriverManager.getConnection(
            "jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
            "1ik013v20-12", "aubergine")) {
        Statement statement = conn.createStatement();
        ResultSet result = statement.executeQuery("SELECT Namn FROM album WHERE Artist = 'Bob Dylan'");
        while (result.next()) {
            låtar.add(result.getString(1));
        }
    } catch (SQLException ex) {
        System.out.println("Something went wrong...");
    }
    return låtar;
}

private ArrayList<String> getLåtar1 () {
    ArrayList<String> låtar = new ArrayList<>();
    try (Connection conn = DriverManager.getConnection(
            "jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
            "1ik013v20-12", "aubergine")) {
        Statement statement = conn.createStatement();
        ResultSet result = statement.executeQuery("SELECT Namn FROM album WHERE Artist = 'Bob Marley'");
        while (result.next()) {
            låtar.add(result.getString(1));
        }
    } catch (SQLException ex) {
        System.out.println("Something went wrong...");
    }
    return låtar;
}
private ArrayList<String> getLåtar2 () {
    ArrayList<String> låtar = new ArrayList<>();
    try (Connection conn = DriverManager.getConnection(
            "jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
            "1ik013v20-12", "aubergine")) {
        Statement statement = conn.createStatement();
        ResultSet result = statement.executeQuery("SELECT Namn FROM album WHERE Artist = 'Bob Marley'");
        while (result.next()) {
            låtar.add(result.getString(1));
        }
    } catch (SQLException ex) {
        System.out.println("Something went wrong...");
    }
    return låtar;
}
private ArrayList<String> getLåtar3 () {
    ArrayList<String> låtar = new ArrayList<>();
    try (Connection conn = DriverManager.getConnection(
            "jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
            "1ik013v20-12", "aubergine")) {
        Statement statement = conn.createStatement();
        ResultSet result = statement.executeQuery("SELECT Namn FROM album WHERE Artist = 'Bob Marley'");
        while (result.next()) {
            låtar.add(result.getString(1));
        }
    } catch (SQLException ex) {
        System.out.println("Something went wrong...");
    }
    return låtar;
}

@Override
public void start(Stage primaryStage) {
    BorderPane pane = new BorderPane();
    ListView art = new ListView();
    art.getItems().addAll(getArtister());
    ListView alb = new ListView();
    ListView alb1 = new ListView();
    ListView alb2 = new ListView();
    ListView alb3 = new ListView();

    art.getSelectionModel().selectedIndexProperty().addListener(ov -> {

    });

    Text text = new Text("MUSIK");
    text.setFont(new Font(20));
    text.setFill(Color.DARKBLUE);
    pane.setLeft(art);
    pane.setTop(text);
    BorderPane.setAlignment(art, Pos.CENTER_LEFT);
    BorderPane.setAlignment(text, Pos.TOP_LEFT);

    Scene scene = new Scene(pane, 500, 500);
    primaryStage.setTitle("Musiken");
    primaryStage.setScene(scene);
    primaryStage.show();
}

}

zzlelutf

zzlelutf1#

好的,试试这个。创建一个新类,如下所示

private class Vinyl
{
    String artist;
    List<String> listAlbums;

    public Vinyl(String artist) {
        this.artist = artist;
        listAlbums = new ArrayList<String>();
    }

    public void AddToListAlbums(String album)
    {
        listAlbums.add(album);
    }

    public String getArtist() {
        return artist;
    }

    public List<String> getListAlbums() {
        return listAlbums;
    }               
}

我刚把它命名为乙烯基。你想叫什么名字都行。
现在回到你的主类,定义一个列表。只调用这些方法来填充列表。

List<Vinyl> listVinyl = new ArrayList<>();  

private void GetArtists () {

    try (Connection conn = DriverManager.getConnection(
            "jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
            "1ik013v20-12", "aubergine")) {
        Statement statement = conn.createStatement();
        ResultSet result = statement.executeQuery("SELECT Namn FROM artist");
        while (result.next()) {
            listVinyl.add(new Vinyl(result.getString(1))); 
        }
    } catch (SQLException ex) {
        System.out.println("Something went wrong...");
    }        
}   

private void GetAlbums () {      
    try (Connection conn = DriverManager.getConnection(
            "jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
            "1ik013v20-12", "aubergine")) {
        Statement statement = conn.createStatement();
        ResultSet result = statement.executeQuery("SELECT Namn FROM album");

        Vinyl currentVinyl;
        String album = "";

        while (result.next()) {

            String artist = result.getString(1); // TODO you must correct the number. I don't know which field is it.

            currentVinyl = listVinyl.stream()
                    .filter(u -> u.artist.equals(artist))
                    .findAny()
                    .orElse(null);

            album = result.getString(2); // TODO you must correct the number. I don't know which field is it.               

            currentVinyl.AddToListAlbums(album);                
        }
    } catch (SQLException ex) {
        System.out.println("Something went wrong...");
    }
}

请小心,有两件事要做。这些是你的字段索引。我不知道你数据库的结构,所以我只写了1和2。这些必须是您的艺术家姓名和专辑名称的字段。
最后,您将只有一个集合,涵盖您需要的所有内容。

相关问题