RxJava/RxAndroid : doOnNext 傷城~ 2023-10-17 17:54 61阅读 0赞 **RxJava/RxAndroid : doOnNext** import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import io.reactivex.Observable; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.functions.Consumer; import io.reactivex.schedulers.Schedulers; public class MainActivity extends AppCompatActivity { private String TAG = "输出"; private Observer<Integer> mObserver = new Observer<Integer>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(Integer i) { Log.d(TAG, "onNext:" + i); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } @Override public void onError(Throwable e) { Log.e(TAG, e.toString(), e); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Integer[] numbers = new Integer[]{0, 1, 2, 3, 4}; Observable.fromArray(numbers) .takeLast(3) .doOnNext(new Consumer<Integer>() { @Override public void accept(Integer integer) throws Exception { Log.d(TAG, "accept:" + integer); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(mObserver); } } output: 01-19 15:01:48.904 11409-11429/zhangphil.book D/输出: accept:2 01-19 15:01:48.906 11409-11429/zhangphil.book D/输出: accept:3 01-19 15:01:48.906 11409-11429/zhangphil.book D/输出: accept:4 01-19 15:01:48.983 11409-11409/zhangphil.book D/输出: onNext:2 01-19 15:01:48.983 11409-11409/zhangphil.book D/输出: onNext:3 01-19 15:01:48.983 11409-11409/zhangphil.book D/输出: onNext:4 01-19 15:01:48.983 11409-11409/zhangphil.book D/输出: onComplete
相关 RxJava/RxAndroid : doOnNext RxJava/RxAndroid : doOnNext import android.support.v7.app.AppCompatActivity; im 傷城~/ 2023年10月17日 17:54/ 0 赞/ 62 阅读
还没有评论,来说两句吧...