0

I am trying to use RXjava to get data from an Arraylist in Android

public ArrayList<String> SelecIDValueGetterObservable(Context mContext) {
        ArrayList<String> SelectedIds = new ArrayList<>();
        CompositeDisposable mCompositeDisposable = new CompositeDisposable();
        mCompositeDisposable.add(Observable.fromCallable(() -> SelecetIDValuegetter(mContext))
                .subscribeOn(Schedulers.io())
               // .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<ArrayList<String>>() {
                    @Override
                    public void onNext(ArrayList<String> IdObsList) {
                        Toast.makeText(mContext,
                                "size " + IdObsList.size(), Toast.LENGTH_LONG).show();
                        
                            for (int i = 0; i < IdObsList.size(); i++) {
                                SelectedIds.add(IdObsList.get(i));
                            }
                      
                    }

                    @Override
                    public void onError(Throwable e) {
                        FirebaseCrashlytics.getInstance().recordException(e);
                    }

                    @Override
                    public void onComplete() {
                        // mCompositeDisposable.dispose();
                    }
                })
        );
        return SelectedIds;
    }

the SelecetIDValuegetter(mContext) is Like this

    public ArrayList<String> SelecetIDValuegetter(Context mContext) {
            ArrayList<String> SelectedIds = new ArrayList<>();
             SelectedIds .add("A");
             SelectedIds .add("B");
             SelectedIds .add("C");
            SelectedIds .add("D");
            return SelectedIds ;
        }

If SelecetIDValuegetter emits data normally, hwoever It deosnot work In Rxjava, how can we do it

1 Answer 1

1

If you want to use Observable, you should not return list directly as Observable is async, instead return Observable and subscribe to receive the data

public Observable<List<String>> SelecIDValueGetterObservable(Context mContext) {
    return Observable.fromCallable(() -> SelecetIDValuegetter(mContext));     
}
//at receiver end
SelecIDValueGetterObservable(this)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(IdObsList -> {
                    //ypu will get the list here
            });

On a side note, Not sure about your need for using Observable in between SelecetIDValuegetter, you can directly return the list instead of using an Observable as you are not doing heavy work in method SelecetIDValuegetter AFAIK.

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

1 Comment

can you explain the .subscribe( part a bit,I am new to Rxjava Trying to learn about it.how do we use it with CompositeDisposable?

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.