在 rxAndroid 中返回对象的 ArrayList
Returning an ArrayList of an object in rxAndroid
我想定期 运行 一个方法,这样它 returns 一个自定义对象的 ArrayList
。这是我的代码片段,
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());
}
});
然而,这给出了一个错误
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>>)
当返回值是 ArrayList<String>
时,这可以正常工作。我不明白这里的问题是什么。是否不允许自定义对象?
地图不订阅,订阅后获取。这是演示它的示例代码。
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
}
});
我想定期 运行 一个方法,这样它 returns 一个自定义对象的 ArrayList
。这是我的代码片段,
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());
}
});
然而,这给出了一个错误
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>>)
当返回值是 ArrayList<String>
时,这可以正常工作。我不明白这里的问题是什么。是否不允许自定义对象?
地图不订阅,订阅后获取。这是演示它的示例代码。
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
}
});