Remove all listeners - or some other solution to the given problem
So the problem is this:
Say we have a list of items displayed in a ListView. The items might consist of a String description, and importantly, a list of ids.
Then we have a group of checkboxes, each associated with an id value. Which checkboxes are selected should be determined by the contents of the list of the selected item in the ListView.
As an example, the items may be persons, and the ids may refer to the person's hobbies or whatever - typical example. So you have a group of predefined hobbies displayed that you can check whether the given person has them or not.
Thus the behaviour we want is:
1) When the selected item in the ListView is changed, update the checkboxes to display the ids of that item.
This is trivial. Simply add a listener on the ListView-selected item property, and for each checkbox, set it to selected if the list contains the corresponding id.
2) When any of the checkboxes is changed by the user, update the list of ids in the ListView-selected item.
This seems to be not as trivial as it seems. :)
The first thing I tried was:
Add a fixed invalidation listener to each of the checkboxes. In the handler, get a list of all ids that are CheckBox-selected, and update the current ListView-selected item's list with these.
The problem here is the when you select an item in the ListView and the behaviour in 1) is invoked, the invalidation handler is called, thus updating the ListView-selected item's list again before it has had a chance to update all the checkboxes!
Next thing I tried:
Don't add any fixed listener to the CheckBoxes. Instead, in 1), remove any listener from the CheckBoxes, update them, then add a listener that will invoke the behaviour in 2).
The problem here lies with "remove any listener from the CheckBoxes", since the removeListener() method on Observable takes a specific listener as parameter - and we don't have any reference to that. (And the first time, we haven't even added any.)
So is there a way to say to an Observable, hey, remove any listeners you might be having?
Or am I on the wrong track and there actually is another, obvious, solution to this problem?