使用RxJava获取对象,转换包含列表,并使用列表

时间:2014-12-08 16:50:49

标签: android rx-java rx-android

我正在尝试解决的高级问题是使用RxJava将获取的Foo(Observable)中包含的FooContainer个对象的列表转换为FooBar个对象的列表。

我的(困惑)尝试:

fooContainerObservable
  .map(container -> container.getFooList())
  .flatMap(foo -> transformFooToFooBar(foo))
  .collect( /* What do I do here? Is collect the correct thing? Should I be using lift? */)
  .subscribe(fooBarList -> /* Display the list */);

我的困惑(或至少有一点)是将展平列表移回列表的步骤。

注意:我正在尝试在Android应用中执行此操作。

1 个答案:

答案 0 :(得分:17)

toList可以将一个Observable的所有项目收集到List并发出它。 toList可以使用collect来实现,但它比collect容易得多。例如,它将是:

fooContainerObservable
  .map(container -> container.getFooList())
  .flatMap(foo -> transformFooToFooBar(foo))
  .toList()
  .subscribe(fooBarList -> /* Display the list */);