How to convert a nested list of double values ​​to a Java class using RxJava?

In my Android client, I get JSON data from the backend:

[ [ 1427378400000, 553 ], [ 1427382000000, 553 ] ] 

Here is the procedure that actually loads the data. I use 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) {} }); } 

How can I map an internal List<Double> to a specific Java class, such as ProductLevel , using RxJava statements ?

 public class ProductLevel { public Double mTimeStamp; public Double mLevel; public ProductLevel(Double timeStamp, Double level) { mTimeStamp = timeStamp; mLevel = level; } } 

Finally, I would like to get the following: List<ProductLevel> .

0
android retrofit rx-java rx-android observable
source share
3 answers

According to your data, you get a list of pairs (time stamp, level). This pair is represented by a list that contains only 2 values.

So, you want to radiate every pair, and convert each pair to ProductLevel .

To do this, you will need a flatMap to specify your pair list in order to emit each pair. Then, before map each pair goes into ProductLevel . Finally, just create a list with all the elements selected.

(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 for each pair .toList() // build a list with all ProductLevel .subscribe(listOfProductLevel -> /** ... **/); 
+2
source share
 List<ProductLevel> productLevels = new ArrayList<>(); for(List<Double> p: list) { productLevels.add(new ProductLevel(p.get(0),p.get(1))); } 

I think it should work.

0
source share

If you modify readProductLevels so that each item in the list is one at a time, you can then respond to them one at a time:

In mProductService:

 private Observable<List<Double>> readProductLevels() { return Observable.create( new Observable.OnSubscribe<List<Double>>() { @Override public void call(Subscriber<? super List<Double>> subscriber) { List<List<Double>> items = new ArrayList<List<Double>>(); List<Double> list1 = new ArrayList<Double>() {{ add(1D); add(2D); }}; items.add(list1); List<Double> list2 = new ArrayList<Double>() {{ add(10D); add(12D); }}; items.add(list2); List<Double> list3 = new ArrayList<Double>() {{ add(21D); add(22D); }}; items.add(list3); // Imagine the list creation above is your Json parsing for (List<Double> list : items) { subscriber.onNext(list); } subscriber.onCompleted(); } }); } 

Allows you to subscribe to every item in your super list:

 private void getProductLevels() { Observable<List<Double>> responseObservable = readProductLevels(); AppObservable.bindFragment(this, responseObservable) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) // TODO: Transform List<List<Double>> into List<ProductLevel> .subscribe( new Subscriber<List<Double>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(List<Double> response) { new ProductLevel(response.get(0), response.get(1)); } }); } 
0
source share

All Articles