Android RxJava 加入列表

Android RxJava joining lists

试图理解所有 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);
}

然后尝试加入两个列表:

 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(),
            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);
                }
            });
在这种情况下,ihuk 建议的

EDIT2 concat 解决方案会好得多。感谢所有答案。

那是因为你试图 return 从 zip 函数中观察到,但是你传递了 Action<String>

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;
                }
            })
            .subscribe(
                    (string)-> System.out.println(string)
            );

我相信您正在寻找的运算符是 concatmerge

Concat 将从两个或多个 Observables 发出排放物而不会 交错。

另一方面,

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

将打印 -1--2--3--4--a--b--c--d-。如果将 concat 替换为 merge,结果将是 -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)-.

    Observable<List<String>> query1(){
        List<String> s = new ArrayList<>();
        s.add("1");s.add("1");s.add("1");
        return Observable.just(s);
    }
    Observable<List<String>> query2(){
        List<String> s = new ArrayList<>();
        s.add("1");s.add("1");s.add("1");
        return Observable.just(s);
    }
    void HelloRx(){
        Map<String,String> map2=new LinkedHashMap<>();//pick the result you want to return  Here !
        Observable.zip(query1(),//Observable Method 1
                query2(),//Observable Method 2
                (result1,result2)->{
                    for(String s : result1){//result1 is the value returned by query1 , result2 ...u know.
                        //do whatever you want
                        //map.put(......)
                    }
                    return null;
                })
                .subscribeOn(BackgroundSchedulers.getMultiThreadInstance())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnCompleted(() -> {
                   //Do Something when finish for example transmit data to your adapter
                })
                .subscribe();
    }

显然要将两个列表合并为一个列表,您可以在它们的 Observable.from() 上执行 Observable.concat(),然后调用 Observable.toList()

RealmResults<Cat> equalTo;
RealmResults<Cat> beginsWith;

@Override
public void onViewRestored() {
    compositeSubscription = new CompositeSubscription();
    equalTo = realm.where(Cat.class).equalTo("field", filterString).findAllSorted("field");
    beginsWith = realm.where(Cat.class).beginsWith("field", filterString).findAllSorted("field");
    compositeSubscription.add(realm.asObservable()
            .switchMap(new Func1<Realm, Observable<Cat>>() {
                @Override
                public Observable<Cat> call(Realm realm) {
                    return Observable.concat(Observable.from(equalTo), Observable.from(beginsWith));
                }
            })
            .toList()
            .subscribe(cats -> {
                // update adapter with List<Cat>
            }));