2
我有一個由Longs表示的時間列表。我想在ListView中顯示這些時間,它調用Longs上的String.format(),使它們變成MM:SS.LLL字符串。我有一個表示以毫秒爲單位的時間的Longs列表。在顯示它們之前,如何創建一個將長格式格式化爲MM:SS.LLL的ListView?
我的想法是這樣的:,
ObservableList<Long> scores = FXCollections.observableArrayList();
//add a few values to scores...
scores.add(123456);
scores.add(5523426);
scores.add(230230478);
//listen for changes in scores, and set all values of formattedScores based on scores values.
ObservableList<String> formattedScores = FXCollections.observableArrayList();
scores.addListener(o -> {
formattedScores.clear();
for (Long score : scores) {
formattedScores.add(String.format("%1$tM:%1$tS.%1$tL", String.valueOf(score)));
}
});
//create an object property that can be bound to ListView.
ObjectProperty<ObservableList<String>> scoresObjProperty = ObjectProperty<ObservableList<String>>(formattedScores);
ListView<String> listView = new ListView<>();
listView.itemsProperty().bind(scoresObjProperty);
我覺得有一個更好的解決方案,但是可能使用Bindings.format()或類似的東西,而無需監聽器重新計算所有的值每一次該列表已更改。