Android RxJava加入列表

kri*_*isk 7 android rx-java

试图了解所有RxJava的东西.我在做以下示例:

private Observable<List<String>> query1() {
    List<String> urls = new ArrayList<>();
    urls.add("1");
    urls.add("2");
    urls.add("3");
    urls.add("4");

    return Observable.just(urls);
}

private Observable<List<String>> query2() {
    List<String> urls = new ArrayList<>();
    urls.add("A");
    urls.add("B");
    urls.add("C");
    urls.add("D");

    return Observable.just(urls);
}
Run Code Online (Sandbox Code Playgroud)

然后尝试加入两个列表:

 Observable.zip(
            query1(),
            query2(),
            new Func2<List<String>, List<String>, Observable<String>>() {
                @Override
                public Observable<String> call(List<String> a1, List<String> a2) {
                    List<String> list = new ArrayList<>();
                    list.addAll(a1);
                    list.addAll(a2);
                    return Observable.from(list);
                }
            })
            .subscribe(new Action1<String>() {  // <-- It says, cannot resolve method subscribe
                @Override
                public void call(String string) {
                    String text = testTextView.getText().toString();
                    testTextView.setText(text + "\n" + string);
                }
            });
Run Code Online (Sandbox Code Playgroud)

我做错了什么?我期待在我看来1 2 3 4 A B C D.

EDIT1我以下面的答案结束了:

    Observable.zip(
            query1(),
            query2(),
            new Func2<List<String>, List<String>, List<String>>() {
                @Override
                public List<String> call(List<String> a1, List<String> a2) {
                    List<String> list = new ArrayList<>();
                    list.addAll(a1);
                    list.addAll(a2);
                    return list;
                }
            })
            .flatMap(new Func1<List<String>, Observable<String>>() {
                @Override
                public Observable<String> call(List<String> urls) {
                    return Observable.from(urls);
                }
            })
            .subscribe(new Action1<String>() {
                @Override
                public void call(String string) {
                    String text = testTextView.getText().toString();
                    testTextView.setText(text + "\n" + string);
                }
            });
Run Code Online (Sandbox Code Playgroud)

concat在这种情况下,ihuk建议的EDIT2解决方案会好得多.感谢所有答案.

ihu*_*huk 26

我相信你正在寻找的运营商是concatmerge.

Concat将在交错Observable情况下从两个或多个s 发射发射.

Merge 另一方面,将通过合并它们的排放来组合多个可观测量.

例如:

    String[] numbers = {"1", "2", "3", "4"};

    String[] letters = {"a", "b", "c", "d"};

    Observable<String> query1 = Observable.from(numbers).delay(1, TimeUnit.SECONDS);
    Observable<String> query2 = Observable.from(letters);

    Observable
            .concat(query1, query2)
            .subscribe(s -> {
                System.out.printf("-%s-" + s);
            });
Run Code Online (Sandbox Code Playgroud)

会打印-1--2--3--4--a--b--c--d-.如果concatmerge结果替换就会-a--b--c--d--1--2--3--4-.

Zipoperator将Observable通过指定的函数将多个s组合在一起.例如

    Observable
            .zip(query1, query2, (String n, String l) -> String.format("(%s, %s)", n, l))
            .subscribe(s -> {
                System.out.printf("-%s-", s);
            });
Run Code Online (Sandbox Code Playgroud)

会输出-(1, a)--(2, b)--(3, c)--(4, d)-.