android - How to transform a nested list of double values into a Java class using RxJava? -
in android client receive json data backend:
[ [ 1427378400000, 553 ], [ 1427382000000, 553 ] ]
here routine loads data. using rxandroid , 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>> 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) {} }); }
how can map inner list<double>
specific java class such productlevel
using rxjava operators?
public class productlevel { public double mtimestamp; public double mlevel; public productlevel(double timestamp, double level) { mtimestamp = timestamp; mlevel = level; } }
finally, receive this: list<productlevel>
.
according data, receive list of pair (timestamp, level). pair represented list contains 2 values.
so want emit each pair, , transform each pair productlevel
.
to this, you'll have flatmap
list of pair emit each pair. map
each pair productlevel
. finally, build list
emited items.
(java8 style)
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 each pair .tolist() // build list productlevel .subscribe(listofproductlevel -> /** ... **/);
Comments
Post a Comment