2017-06-30 29 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()名單上