Thursday, 25 April 2019

RxAndroid | Reactivex.Observable | How to create a Observable which can emit String

Dependencies :-
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
// (see https://github.com/ReactiveX/RxJava/releases for latest 2.x.x version)    
implementation 'io.reactivex.rxjava2:rxjava:2.2.1'
MainActivity.java :-
Observable<String> observable;
Observer<String> observer;
@Override 
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    observable = Observable.just("Android Hubbbb");
    observer = new Observer<String>() {
        @Override        
           public void onSubscribe(Disposable d) {

        }

        @Override        
           public void onNext(String s) {
            tvText.setText(s);
        }

        @Override        
          public void onError(Throwable e) {

        }

        @Override        
           public void onComplete() {

        }
    };

    buttob.setOnClickListener(new View.OnClickListener() {
        @Override         
           public void onClick(View v) {
           observable.subscribe(observer);
        }
    });

No comments:

Post a Comment