Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

java - JavaFX, Casting ArrayList to ObservableList

Is there a way to cast ArrayList to ObservableList? I would like to do it without iterating through ArrayList.

To be more specific, I am using ORMLite to get data from database, and I need ObservableList as an output of the method fetching data from DB.

Currently I am doing something like this:

ArrayList<Stavka> listStavaka = new ArrayList<>(); 
Dao<Stavka, Integer> stavkaDao = DaoManager.createDao(connection, Stavka.class);
listStavaka = (ArrayList<Stavka>) stavkaDao.queryForAll();
ObservableList<Stavka> oListStavaka = FXCollections.observableArrayList();
for (Stavka stavka : listStavaka) {
    oListStavaka.add(stavka);
}

And I would like to do something like this:

ObservableList<Stavka> listStavaka = FXCollections.observableArrayList();
Dao<Stavka, Integer> stavkaDao = DaoManager.createDao(connection, Stavka.class);
listStavaka = (ObservableList<Stavka>) stavkaDao.queryForAll();
question from:https://stackoverflow.com/questions/22191954/javafx-casting-arraylist-to-observablelist

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do

ObservableList<Stavka> oListStavaka = FXCollections.observableArrayList(listStavaka);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...