android-studiorx-javaandroid-spinnerrx-java2rx-binding

How to send Spinner item value to multiple subscibers using Rxbinding


Android :

What I've implemented till now is, get spinner value to the immediate subscriber using itemSelection(). Code :

    Observavle observavle = RxAdapterView.itemSelections(spinner);
    observable.subscribeOn(AndroidSchedulers.mainThread())
    .subscribe(integer -> {
    Log.v("spinner", integer.toString());
    });

What I want to achieve is :

I want to subscribe multiple observers to this Observable. For which I have created few Observers and used

    Observable.subscribe(observer1);
    Observable.subscribe(observer2);
    Observable.subscribe(few more observers);

To get spinner updated value on these observers, but this is not working, OnNext() of these observers won't get called on spinner value change.

(Note: This situation works perfectly in case of RxBinding Textview using on textchange() ).

Will surely upvote if you can help me with this.


Solution

  • You can use the share() operator:

    Observavle observavle = RxAdapterView.itemSelections(spinner).share();
    

    You can read more about share() in this blog post.