简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM