简体   繁体   English

从ListView获取所有项目并转换为字符串Javafx?

[英]Get all items from ListView and convert to string Javafx?

I have a ListView in my Netbeans 8.1 Javafx project, and I wish to retrieve all the items from a ListView that have been added to that and have them in a String. 我的Netbeans 8.1 Javafx项目中有一个ListView,我希望从ListView中检索所有已添加到其中的项目,并将它们放在字符串中。 I do not just want the selected item in the ListView, I want every item. 我不仅想要ListView中的选定项目,还想要每个项目。

Apologies that I have no attempt at code, as I really do not know how to go about this. 我没有尝试过代码的道歉,因为我真的不知道该怎么做。 Thank you in advance for your help and time. 预先感谢您的帮助和时间。

Using the Collectors class (example is just copy and pasted from there). 使用Collectors类(示例只是从那里复制并粘贴)。

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

Well, ListView does have a method called getItems(). 好吧,ListView确实有一个名为getItems()的方法。 You might want to start with that. 您可能要从此开始。

By converting from a Observablelist to a List, I was easily able to convert it to a String to suit my needs. 通过将Observablelist转换为List,我可以轻松地将其转换为String以适应我的需求。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM