创建发送指定值的Observerble,just只是简单的原样发射,将数组或Iterable当做单个数据。如果传递的值为null,则发送的Observable的值为null。参数最多为9个
运行的结果:
二、from方法
将数据转换成为Observables,而不是需要混合使用Observables和其它类型的数据
String [] items = {"just1","just1","just1","just1","just1","just1"}; Observable<String> myObservable = Observable.fromArray(items); Subject <String> subject = new Subject<String>() { @Override public boolean hasObservers() { return false; } @Override public boolean hasThrowable() { return false; } @Override public boolean hasComplete() { return false; } @Override public Throwable getThrowable() { return null; } @Override protected void subscribeActual(Observer<? super String> observer) { } @Override public void onSubscribe(Disposable d) { } @Override public void onNext(String value) { System.out.println("onCompleted................."+value); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }; myObservable.subscribe(subject); }09-05 15:05:05.459 9427-9427System.out: onCompleted.................just1
09-05 15:05:05.459 9427-9427System.out: onCompleted.................just1
09-05 15:05:05.459 9427-9427/System.out: onCompleted.................just1
09-05 15:05:05.459 9427-9427System.out: onCompleted.................just1
09-05 15:05:05.459 9427-9427System.out: onCompleted.................just1
09-05 15:05:05.459 9427-9427System.out: onCompleted.................just1
三、repeat方法
1.repeat()重复地执行某个操作,如果不传递参数,结果将会被无限地重复执行,默认在trampoline
调度器上执行,该方法为非静态方法,不可以直接通过Observable来调用
String [] items = {"just1","just1","just1","just1","just1","just1"}; Observable<String> myObservable = Observable.fromArray(items).repeat();; Subject <String> subject = new Subject<String>() { @Override public boolean hasObservers() { return false; } @Override public boolean hasThrowable() { return false; } @Override public boolean hasComplete() { return false; } @Override public Throwable getThrowable() { return null; } @Override protected void subscribeActual(Observer<? super String> observer) { } @Override public void onSubscribe(Disposable d) { } @Override public void onNext(String value) { System.out.println("onCompleted................."+value); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }; myObservable.subscribe(subject); }