2
私はLongsで表される時間のリストを持っています。私はListViewでこれらの時間をMM:SS.LLL文字列にするためにLongs上でString.format()を呼び出すようにします。私は時間をミリ秒単位で表すLongsのリストを持っています。 MMを表示する前にLongsをMM:SS.LLLにフォーマットするListViewを作成するにはどうすればよいですか?
私はこのような何かを考えていた:より良い解決策があるようにリスナーを持つことなく、おそらくBindings.format()または同様のものを使用して、しかし、私は感じ
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);
は毎回すべての値を再計算しますリストが変更されます。