Android RxJava加入列表

前端之家收集整理的这篇文章主要介绍了Android RxJava加入列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
试图了解所有Rx Java的东西.我正在做以下示例:
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);
}

然后尝试加入两个列表:

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);
                }
            });

我做错了什么?我期待着进入我的视野
1
2
3
4
一个

C
d

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

Observable.zip(
            query1(),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);
                }
            });

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

解决方法

我相信你正在寻找的操作符是concat或merge.

Concat将从两个或多个Observable发射排放,而不会交错.

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

例如:

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);
            });

将打印-1–2–3–4 – a – b – c – d-.如果用conc替换concat,结果将是-a-b-c-d-1–2–3–4-.

Zip运算符将通过指定的函数将多个Observable组合在一起.例如

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

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

原文链接:https://www.f2er.com/android/317663.html

猜你在找的Android相关文章