简体   繁体   English

返回rxAndroid中对象的ArrayList

[英]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. 我想定期运行一个方法,以便它返回一个自定义对象的ArrayList 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>>) map(rx.functions.Func1<? super T, ? extends R>)无法应用于(anonymous rx.functions.Func1<java.lang.Long, java.util.ArrayList<com.example.Item>>)

This works fine when the returned value is an ArrayList<String> . 当返回的值是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
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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