2016-03-25 56 views
2

我的代码如下。如何将每个项目添加到RxJava列表中Android

Observable<List<Appointment>> callAppointments = appointmentServiceRx.appointmentService.getConfirmedAppointments(user_id); 
    callAppointments 
      .flatMapIterable(appointments -> appointments) 
      .flatMap(app -> Observable.zip(
        app, 
        patientServiceRx.patientService.getPatientById(app.patient_id), 
        servicesRestRx.servicesAPIServiceRx.getSubserviceById(app.subservice_id), 
        (appointment, patient, subservice) -> this.createListItem(appointment, patient, subservice) 
      )) 
      .toList() 
      .subscribeOn(Schedulers.io()) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .subscribe(); 

随着helper方法如下

private Observable<AppointmentListItem> createListItem (Appointment app, Patient patient, Subservice subservice){ 
    AppointmentListItem item = new AppointmentListItem(app.appointment_id, subservice.subservice_name, 
                 patient.patient_fullname, app.appointment_datetime); 
    return Observable.just(item); 
} 

我得到了一个错误,指出预期的参数和实际参数时,我试着打createListItem在Observable.zip

不匹配这是错误消息。

Error message

请帮助.....

回答

0

Timeline: Activity_idle是一个红色的鲱鱼和无关您的问题。

看来你使用了一个血腥的字段appointment!你只有一个AppointmentListItem实例,这就是为什么!

编辑:Observable::zip是你的朋友!以下是我该怎么做:

callAppointments 
     .flatMapIterable(appointments -> appointments) 
     .flatMap(app -> Observable.zip(
      app, 
      patientServiceRx.getService().getPatientById(app.patient_id), 
      servicesRestRx.getService().getSubserviceById(app.subservice_id), 
      (app, patient, subservice) -> this.createListItem(app, patient, subservice)) 
     .toList() 
     .subscribeOn(Schedulers.io()) 
     .observeOn(AndroidSchedulers.mainThread()) 
     .subscribe(list -> {/*set adapter*/}, error -> {/* handle error */}); 

这有两个并行的预约电话的额外好处。请记住,flatMap不一定会保留原始顺序,所以您可能会以与Appointments不同的顺序结束AppointmentListItem。

+0

啊我明白了。那么如何将每个结果调用检索到Appointment对象而不是使用字段呢?任何建议? –

+0

我编辑了原文。我也想知道为什么你使用'getService()'调用;为什么不直接注入服务? –

+0

可否请您解释更多关于直接注入服务的信息?我仍然是这种东西的新手。 –

相关问题