【问题标题】:Get all items from ListView and convert to string Javafx?从 ListView 获取所有项目并转换为字符串 Javafx?
【发布时间】:2017-03-22 18:28:45
【问题描述】:

我的 Netbeans 8.1 Javafx 项目中有一个 ListView,我希望从 ListView 中检索已添加到其中的所有项目,并将它们放在一个字符串中。 我不只是想要 ListView 中的选定项,我想要每个项。

抱歉,我没有尝试编写代码,因为我真的不知道该怎么做。 提前感谢您的帮助和时间。

【问题讨论】:

    标签: java string listview javafx netbeans-8


    【解决方案1】:

    使用Collectors 类(示例只是从那里复制和粘贴)。

     // Convert elements to strings and concatenate them, separated by commas
     String joined = listView.getItems().stream()
                           .map(Object::toString)
                           .collect(Collectors.joining(", "));
    

    【讨论】:

      【解决方案2】:

      嗯,ListView 确实有一个名为 getItems() 的方法。你可能想从那开始。

      【讨论】:

      • 字符串列表 = new String(); list = listView.getItems();这会引发错误“不兼容的类型:ObservableList 无法转换为字符串”
      • @JustReflektor 那么它返回了什么?你试图用它返回的东西做什么?你有没有在任何地方寻找任何有用的方法?
      【解决方案3】:

      通过从 Observablelist 转换为 List,我可以轻松地将其转换为 String 以满足我的需要。

      List<String> numbers = listView.getItems();
      String listString = String.join(", ", numbers);
      

      【讨论】:

      • “转换”不准确。 ObservableList 已经是 List。 (你可以写String listString = String.join(", ", listView.getItems());)。顺便说一句,如果您有额外的信息对提供解决方案很有用,例如您正在处理ListView&lt;String&gt;,您应该在您的问题中包含这些信息。
      猜你喜欢
      • 2021-07-11
      • 2012-11-17
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      相关资源
      最近更新 更多