To listen ObservableList I am using the following code in listener:
@Override
public void onChanged(ListChangeListener.Change<? extends MyClass> c) {
for(MyClass s : c.getAddedSubList()) {
// process s
}
}
unfortunately, I am getting a lot of exceptions:
java.lang.IllegalStateException: Invalid Change state: next() must be called before inspecting the Change.
Should I call next() before my loop? But this contradicts the statement from ListChangeListener javadoc saying getList() should work at any time, and that getAddedSubList() is just a shortcut of getList() expression.
A change event may be fired for a single event that cannot be represented by a single set of results from ListChangeListener.Change.getXXX() methods. (For example, if you call setAll(...) on your observable list, under some circumstances.) To account for such changes you have to iterate through a collection of "change representations", getting the information for each of them. The proper idiom to use is
@Override
public void onChanged(ListChangeListener.Change<? extends MyClass> c) {
while (c.next()) {
if (c.wasAdded()) {
for(MyClass s : c.getAddedSubList()) {
// process s
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With