在我的Android客户端我收到此JSON数据从后端:
In my Android client I receive this JSON data from a backend:
[
[
1427378400000,
553
],
[
1427382000000,
553
]
]
下面是实际加载数据的程序。我使用 RxAndroid 和的改造这里。
Here is the routine which actually loads the data. I am using RxAndroid and Retrofit here.
private void getProductLevels() {
Observable<List<List<Double>>> responseObservable =
mProductService.readProductLevels();
AppObservable.bindFragment(this, responseObservable)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
// TODO: Transform List<List<Double>> into List<ProductLevel>
.subscribe(new Subscriber<List<List<Double>>>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable e) {}
@Override
public void onNext(List<List<Double>> response) {}
});
}
我该如何映射内名单,其中,双&GT;
到一个特定的Java类,如 ProductLevel
使用的 RxJava运营商
How can I map the inner List<Double>
to a specific Java class such as ProductLevel
using RxJava operators?
public class ProductLevel {
public Double mTimeStamp;
public Double mLevel;
public ProductLevel(Double timeStamp, Double level) {
mTimeStamp = timeStamp;
mLevel = level;
}
}
最后,我希望收到这样:名单,其中,ProductLevel&GT;
推荐答案
根据您的资料,您会收到对(时间戳,级别)的列表。这对被重新presented由只包含两个值的列表。
According to your Data, you receive a list of pair (timestamp, level). This pair is represented by a list which contains only 2 values.
所以,你要为 EMIT 每一对,和转换每对成 ProductLevel
。
So you want to emit each pair, and transform each pair into a ProductLevel
.
要做到这一点,你必须 flatMap
您对列表发出的一对。然后到地图
每对成 ProductLevel
。最后,只是建立一个列表
所有emited项目。
To do this, you'll have to flatMap
your list of pair to emit each pair. Then to map
each pair into a ProductLevel
. Finally, just build a list
with all emited items.
(java8风格)
AppObservable.bindFragment(this, responseObservable)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMapIterable(listOfList -> listOfList) // or flatMap(l -> Observable.from(l))
.map(pair -> new ProductLevel(pair.get(0),pair.get(1))) // build ProductLevel for each pair
.toList() // build a list with all ProductLevel
.subscribe(listOfProductLevel -> /** ... **/);
相关推荐
最新文章