你如何在 RxJava 的 Observable 中迭代一个列表?
How do you iterate over a list within an Observable in RxJava?
我目前正在学习 RxJava/RxAndroid 的基础知识,并尝试创建一个非常基础的 Retrofit 测试应用程序。
我在这个例子中使用 RxJava 1.2.6。
但是,我无法找到任何明确的示例来说明如何对 在 Observable 对象中的列表的对象执行函数。
例如如果我有以下 POJO
public class AgencyResponse {
private List<Agency> agencies;
private int total;
private int count;
private int offset;
public List<Agency> getAgencies() {
return agencies;
}
public int getTotal() {
return total;
}
public int getCount() {
return count;
}
public int getOffset() {
return offset;
}
}
以及
public class Agency {
private int id;
private String name;
private String abbrev;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAbbrev() {
return abbrev;
}
}
当 Observable 对象是包含列表。 IE。 Observable<AgencyResponse>
一个真实的世界,希望更清楚的例子如下:
public class MainActivity extends BaseActivity {
private TextView textView; // Code to populate this omitted
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create Retrofit service
LaunchLibService service = ServiceFactory.createRetrofitService(LaunchLibService.class, LaunchLibService.SERVICE_ENDPOINT);
//getAgencyList returns Observable<AgencyResponse>
service.getAgencyList()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.map(AgencyResponse::getAgencies) // Gets the List<Agency> but how do I iterate over each object and print?
.subscribe();
}
}
任何帮助都将非常有用,谢谢。
使用以下方法解决了这个问题 - 我一直缺少 flatMapIterable
可以访问 Agency
对象。
LaunchLibService service = ServiceFactory.createRetrofitService(LaunchLibService.class, LaunchLibService.SERVICE_ENDPOINT);
service.getAgencyList()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.map(AgencyResponse::getAgencies) // Gets the List<Agency> but how do I iterate over each object
.flatMapIterable(agencyResponse -> agencyResponse)
.map(Agency::getName)
.subscribe(s -> textView.append(s + "\n"));
我目前正在学习 RxJava/RxAndroid 的基础知识,并尝试创建一个非常基础的 Retrofit 测试应用程序。
我在这个例子中使用 RxJava 1.2.6。
但是,我无法找到任何明确的示例来说明如何对 在 Observable 对象中的列表的对象执行函数。
例如如果我有以下 POJO
public class AgencyResponse {
private List<Agency> agencies;
private int total;
private int count;
private int offset;
public List<Agency> getAgencies() {
return agencies;
}
public int getTotal() {
return total;
}
public int getCount() {
return count;
}
public int getOffset() {
return offset;
}
}
以及
public class Agency {
private int id;
private String name;
private String abbrev;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAbbrev() {
return abbrev;
}
}
当 Observable 对象是包含列表。 IE。 Observable<AgencyResponse>
一个真实的世界,希望更清楚的例子如下:
public class MainActivity extends BaseActivity {
private TextView textView; // Code to populate this omitted
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create Retrofit service
LaunchLibService service = ServiceFactory.createRetrofitService(LaunchLibService.class, LaunchLibService.SERVICE_ENDPOINT);
//getAgencyList returns Observable<AgencyResponse>
service.getAgencyList()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.map(AgencyResponse::getAgencies) // Gets the List<Agency> but how do I iterate over each object and print?
.subscribe();
}
}
任何帮助都将非常有用,谢谢。
使用以下方法解决了这个问题 - 我一直缺少 flatMapIterable
可以访问 Agency
对象。
LaunchLibService service = ServiceFactory.createRetrofitService(LaunchLibService.class, LaunchLibService.SERVICE_ENDPOINT);
service.getAgencyList()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.map(AgencyResponse::getAgencies) // Gets the List<Agency> but how do I iterate over each object
.flatMapIterable(agencyResponse -> agencyResponse)
.map(Agency::getName)
.subscribe(s -> textView.append(s + "\n"));