How to know which class's Observable field fired the change?

28 views
Skip to first unread message

Csaba Troják

unread,
Feb 5, 2014, 3:06:38 PM2/5/14
to android...@googlegroups.com
Let's assume I have an ArrayListObservable<CustomVM>, and I want to sign up for every CustomVM's IsSelected field's changed event.

So I have something like this:

_isSelectedChange= new Observer() {
@Override
public void onPropertyChanged(IObservable<?> prop, Collection<Object> initiators) {
}
};

for(CustomVM actVM : viewModels)
{
actVM.IsSelected.subscribe(_isSelectedChange);
}

In the "onPropertyChanged" how is it possible to access the viewmodel whose property fired the event? Neither the prop nor the initiator helps :(

Csaba Troják

unread,
Feb 5, 2014, 3:25:15 PM2/5/14
to android...@googlegroups.com
I made the following code, however I don't really like it :(

_isSelectedChange = new Observer() {
@Override
public void onPropertyChanged(IObservable<?> prop, Collection<Object> initiators) {
IObservable<Boolean> actProp = (IObservable<Boolean>)prop;
if(actProp.get())
{
for(CustomVM actVM : _viewModels)
{
if(actProp != actVM.IsSelected)
{
actVM.IsSelected.unsubscribe(_isSelectedChange);
actVM.IsSelected.set(false);
actVM.IsSelected.subscribe(_isSelectedChange);
}
}
}
}
};
Reply all
Reply to author
Forward
0 new messages