2017-06-30 26 views
-1

我正在制作汇率应用程序,并且屏幕上显示的图表显示了过去7天内所选货币的变化情况。RxJava2按顺序排放物品

现在我想要得到的是严格排序项目。

这里是我的代码:

public class GraphInteractorImpl implements GraphInteractor { 

private final Retrofit retrofit; 


@Inject 
public GraphInteractorImpl(Retrofit retrofit) { 
    this.retrofit = retrofit; 
} 

@Override 
public void downloadData(GraphListener listener) { 

    RestAPI api = retrofit.create(RestAPI.class); 

    List<String> listDates = getDates(); 


    for (String date : listDates) { 

     Observable<List<ExchangeRate>> observable = api.getExchangeRatesForLast7days(date); 


     observable.subscribeOn(Schedulers.io()) 
       .observeOn(AndroidSchedulers.mainThread()) 
       .subscribe(
         listener::onSuccess, 
         listener::onFailure 

       ); 
    } 


} 


private List<String> getDates() {  //returns last 7 days in a list 

    List<String> listDate = new ArrayList<>(); 

    Calendar calendarToday = Calendar.getInstance(); 
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); 
    String today = simpleDateFormat.format(calendarToday.getTime()); 

    Calendar calendarDayBefore = Calendar.getInstance(); 
    calendarDayBefore.setTime(calendarDayBefore.getTime()); 

    int daysCounter = 0; 

    while (daysCounter <= 7) { 

     if (daysCounter == 0) { // means that its present day 
      listDate.add(today); 
     } else {      // subtracts 1 day after each pass 
      calendarDayBefore.add(Calendar.DAY_OF_MONTH, -1); 
      Date dateMinusOneDay = calendarDayBefore.getTime(); 
      String oneDayAgo = simpleDateFormat.format(dateMinusOneDay); 

      listDate.add(oneDayAgo); 

     } 

     daysCounter++; 
    } 

    return listDate; 

} 
} 

此代码让我正确的价值观,但他们不是为了使我得到错误值的具体日期。

所以我要做的是同时执行7个调用,我猜测与zip操作符,但我没有拿出这个解决方案,所以任何类型的帮助将不胜感激。

API文档可以在这里找到:http://hnbex.eu/api/v1/

回答

0

所以我做了什么来解决这个是我添加了所有在列表中的7个观测,然后我就叫了zipIterable()名单上