Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an ArrayList of an object in rxAndroid

I want to run a method periodically such that it returns an ArrayList of a custom object. Here is my code snippet,

    subscribe = Observable.interval(5, TimeUnit.SECONDS)
            .map(new Func1<Long, ArrayList<Item>>() {

                @Override
                public ArrayList<Item> call(Long aLong) {
                    return new ArrayList<Item>(aLong.intValue());
                }
            });

However, this gives an error

map(rx.functions.Func1<? super T, ? extends R>)in Observable cannot be applied to (anonymous rx.functions.Func1<java.lang.Long, java.util.ArrayList<com.example.Item>>)

This works fine when the returned value is an ArrayList<String>. I do not understand what the problem is here. Are custom objects not allowed?

like image 925
yadav_vi Avatar asked Dec 15 '25 18:12

yadav_vi


1 Answers

You don't get subscription on map, you get it after subscribing. Here's sample code for demonstrating it.

  Observable<ArrayList<Item>> observable = Observable.interval(5, TimeUnit.SECONDS)
            .map(new Func1<Long, ArrayList<Item>>() {

                @Override
                public ArrayList<Item> call(Long aLong) {
                    return new ArrayList<Item>(aLong.intValue());
                }
            });
    Subscription subscription = observable.subscribe(new Action1<ArrayList<Item>>() {
        @Override
        public void call(ArrayList<Item> items) {
            //Do something with list items here
        }
    });
like image 51
jimmy0251 Avatar answered Dec 17 '25 09:12

jimmy0251



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!