onNext没有被调用

时间:2018-07-17 11:29:05

标签: android rx-java rx-java2 rx-android

如以下代码所示,我想使用RxAndroid。但是当我运行代码时,我没有收到来自

的任何日志
            @Override
            public void onNext(String s) {
                Log.d(TAG, "Name: " + s);
            }

请让我知道如何解决这些问题。

代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Observer<String> animalsObserver = getAnimalsObserver();
        getAnimalsObservable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new Func1<String, Boolean>() {
                    @Override
                    public Boolean call(String s) {
                        return s.toUpperCase().equals("D");
                    }
                })
                .subscribe(animalsObserver);
    }

    private Observable getAnimalsObservable() {
        return Observable.from(Arrays.asList(
                new String[] {
                        "Ant", "Ape",
                        "Bat", "Bee", "Bear", "Butterfly",
                        "Cat", "Crab", "Cod",
                        "Dog", "Dove",
                        "Fox", "Frog"
                }
                ));
    }

    private Observer<String> getAnimalsObserver() {
        return new Observer<String>() {
            @Override
            public void onNext(String s) {
                Log.d(TAG, "Name: " + s);
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onCompleted() {
                Log.d(TAG, "All items are emitted!");
            }
        };
    }
}

1 个答案:

答案 0 :(得分:2)

return s.toUpperCase().equals("D");

这始终是错误的。您可能要检查startsWith。您可以阅读更多here