2

As shown in the following code, I would like to use RxAndroid. but I am facing two issues:

1- I want to convert the Array to observable, but the method .fromArray is not recognised

2- Why in the interface of Observer i dont have onSubscribe(Disposable d) implemented?

please let me know hopw to fix these two issues.

build gradle

compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.9'

code:

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

    //fromArray is not defined
  private Observable<String> getAnimalsObservable() {
    return Observable.fromArray(
            "Ant", "Ape",
            "Bat", "Bee", "Bear", "Butterfly",
            "Cat", "Crab", "Cod",
            "Dog", "Dove",
            "Fox", "Frog");
}

//why in the following interface, i dont have onSubscribe(Disposable d)
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!");
        }
    };
}
3
  • 2
    Are you intend to use RxJava 1 or 2. You are mixing up the two in your last 2 questions. Commented Jul 17, 2018 at 10:46
  • @akarnokd please hae alook at the question again, i updated it. the gradle and the imports are included Commented Jul 17, 2018 at 10:52
  • You are using RxJava 1 where the operator is named from. onSubscribe(Disposable) was introduced in RxJava 2 because of a complete rewrite and rearchitecting of the components. Please make sure you read the javadocs and look at what your IDE's code completion suggests. Commented Jul 17, 2018 at 11:55

4 Answers 4

1

You'd better upgrade your rxjava, actually you're using rxjava1.

check latest version

Then, fromArray should work and you will deal with DisposableObserver interface or Observer interface(rxjava >= 2.16).

Note that onSubscribe(Disposable d) is defined inside Observer interface.

hope i will help you

Sign up to request clarification or add additional context in comments.

Comments

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

you can subscribe like this

getAnimalsObservable().subscribe(i -> {
    // Do some thing with 'i', it's a list of String.
});

or you can do like this as well

Observable.from(Arrays.asList(new String[] {"1", "2", "3", }))
.subscribe(i -> {
    // Do some thing with 'i', it's a list.
});

2 Comments

would yu please answer the 2nd question as well
would you please explain, why the return value from getAnimalsObservable() is not Observable<String>[]. Also, still i dont know why i cant use onSubscribe(disposable d)
0

You are using RxJava 1. I don't remember it clearly, but why not RxJava 2? On RxJava github:

The 1.x version is end-of-life as of March 31, 2018. No further development, support, maintenance, PRs and updates will happen. The Javadoc of the very last version, 1.3.8, will remain accessible.

Maybe 2nd version will help to solve your problem

Comments

0

use fromIterable

example:

val arraylist: ArrayList<CustomObject>
Observable.fromIterable(arraylist)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.