1 回答

TA貢獻1842條經驗 獲得超13個贊
在尋找對我的問題的評論和缺點后,我發(fā)現我需要自己在代碼中搜索問題。希望有人會發(fā)現這個問題并回答有用。問題出在我的閱讀方法中readDataFromStream()。在閱讀上述主題之前,我使用了這種方式聲明ObservableList
public static ObservableList<EmailData> emailData = FXCollections.observableArrayList();
我的readDataFromStream樣子:
public static ObservableList<EmailData> readDataFromStream(byte[] bytes) {
ObservableList<EmailData> emailData = FXCollections.observableArrayList();
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
List<EmailData> list = (List<EmailData>) ois.readObject();
emailData = FXCollections.observableList(list);
bis.close();
ois.close();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
return emailData;
}
然后我讀到我需要使用提取器創(chuàng)建列表。我將ObservableList聲明更改為:
ObservableList<EmailData> emailData = FXCollections.observableArrayList(ed ->
new Observable[]{
ed.nameProperty(),
ed.loginProperty(),
ed.passwordProperty(),
ed.commentsProperty()
});
但我的readDataFromStream()方法保持不變?,F在我把它改成:
public static ObservableList<EmailData> readDataFromStream(byte[] bytes) {
ObservableList<EmailData> emailData = FXCollections.observableArrayList(ed ->
new Observable[]{
ed.nameProperty(),
ed.loginProperty(),
ed.passwordProperty(),
ed.commentsProperty()
});
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
List<EmailData> list = (List<EmailData>) ois.readObject();
emailData = FXCollections.observableList(
list,
(EmailData tp) -> new Observable[]{tp.nameProperty(), tp.passwordProperty(),
tp.loginProperty(), tp.commentsProperty()});
bis.close();
ois.close();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
return emailData;
}
和wasUpdated()方法ListChangeListener.Change 現在就像一個魅力。
希望對我這樣的新手有所幫助。
添加回答
舉報