Android RxJava加入列表

时间:2015-03-03 22:33:06

标签: 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);
}

然后尝试加入两个列表:

 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解决方案会好得多。感谢所有答案。

4 个答案:

答案 0 :(得分:24)

我相信您要查找的运营商为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);
            });

将打印-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)-

答案 1 :(得分:1)

那是因为你试图从zip函数返回Observable,但是你传递了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)
            );

答案 2 :(得分:0)

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

答案 3 :(得分:0)

显然,要将两个列表合并到一个列表中,您可以在Observable.concat()上执行Observable.from(),然后拨打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>
            }));