2017-04-04 69 views
0

如何通過排序接收功能我的列表進行排序,我的目錄包含三種類型不同的來源,然後我要顯示我的名單使用日期排序,如何申請,使用RxAndroid?如何使用RxAndroid

subscriptions.add(complaintsAPI.getComplaintsAPI(userDetails.getUsername()) 
      .compose(ReactiveUtils.applySchedulers()) 
      .map(list -> { 
       List<ComplaintsRowModel> rowModel = new ArrayList<>(); 
       for (Complaint complaint : list.getComplaints()) { 
        rowModel.add(new ComplaintsRowModel(complaint.getDisputeNo(), 
          complaint.getOpenDate(), complaint.getArea(), complaint.getStatus())); 

        model.complaintsList.put(complaint.getDisputeNo(), complaint); 
       } 


       for (OnlineRequest onlineRequest : list.getOnlineRequests()) { 
        rowModel.add(new ComplaintsRowModel(onlineRequest.getRequestNo(), onlineRequest.getOpenDate(), 
          onlineRequest.getArea(), onlineRequest.getStatus())); 

        model.complaintsList.put(onlineRequest.getRequestNo(), onlineRequest); 
       } 

       for (LlTickets llTickets : list.getLlTickets()) { 
        rowModel.add(new ComplaintsRowModel(llTickets.getTicketNo(), llTickets.getOpenDate(), 
          llTickets.getType(), llTickets.getStatus())); 

        model.complaintsList.put(llTickets.getTicketNo(), llTickets); 
       } 


       return rowModel;} 
      ).toSortedList(){ 
     //how to sort here 
    }).subscribe(new RequestSubscriber<List<ComplaintsRowModel>>(view.getContext(), view.progressView) { 

      @Override 
       public void onFailure(RequestException requestException) { 
        view.showError(requestException); 
       } 

       @Override 
       public void onNoData() { 
        super.onNoData(); 
        isAllDataLoaded = true; 
        view.noDataFound(); 
        model.setNoDataFound(true); 
       } 

       @Override 
       public void onNext(List<ComplaintsRowModel> complaintsRowModels) { 
        isAllDataLoaded = true; 
        model.setRowModel(complaintsRowModels); 
        view.buildList(complaintsRowModels); 
       } 
      })); 

我想在toSortedList()我可以整理我的列表,但我不知道以應用的方式。

+0

請出示您的模型 - 'LlTickets','OnlineRequest','Complaint','ComplaintsRowModel'以及要如何排序呢細節(最好用例子)解釋。否則你的問題沒有意義。 – Divers

+0

無需模型的細節只是我想知道如何分揀過程中使用的Rx應用,那種片斷,謝謝 – 3zcs

回答

2

toSortedList運營商只能在Observable<ComplaintRowModel>工作,而你有什麼是Observable<List<ComplaintRowModel>>。所以,首先你必須改變你觀察到的與

flatMapIterable(complaintRowModels -> complaintRowModels) 

其映射到可觀察到的列表中的元素。然後你可以應用排序類似

toSortedList((complaintRowModel, complaintRowModel2) -> { 
    Date date = complaintRowModel.getDate(); 
    Date date2 = complaintRowModel2.getDate(); 

    // comparing dates is very much dependent on your implementation 
    if (date <before> date2) { 
     return -1; 
    } else if (date <equal> date2) { 
     return 0; 
    } else { 
     return 1; 
    } 
}) 

然後你得到一個可觀察的排序列表。

+0

'日期 Divers

+0

不是,它在很大程度上取決於哪種格式的日期被保存 – Lamorak

+0

在你的代碼是'日期date',這就是爲什麼它的混亂。 – Divers

0

按照你不想提供問題的具體信息,有通用的答案。

  1. 當其需要的數據對象進行排序工具Comparable或它的原始類型。

    Observable.just(3, 2, 1) 
         .toSortedList() 
         .subscribe(list -> System.out.print(Arrays.toString(list.toArray()))); 
    

[1,2,3]

  • 當需要被排序的數據對象沒有實現Comparable或實現它,但你需要指定你想如何排序數據。
  • 該示例說明如何按降序排列val字段對對象列表進行排序。

    static class ToSort { 
        Integer val; 
    
        public ToSort(Integer val) { 
         this.val = val; 
        } 
    
        @Override 
        public String toString() { 
         return "ToSort{" + 
           "val=" + val + 
           '}'; 
        } 
    } 
    
    public static void main(String[] args) { 
        Observable.just(new ToSort(1), new ToSort(2), new ToSort(3)) 
          .toSortedList((o1, o2) -> (-1) * o1.val.compareTo(o2.val)) 
          .subscribe(list -> System.out.print(Arrays.toString(list.toArray()))); 
    } 
    

    [ToSort {VAL = 3},ToSort {VAL = 2},ToSort {VAL = 1}]

    0

    不同的方法來replce的

    // comparing dates is very much dependent on your implementation 
    if (date <before> date2) { 
        return -1; 
    } else if (date <equal> date2) { 
        return 0; 
    } else { 
        return 1; 
    } 
    

    使用一個static import java.util.Comparator.comparing; 你可以使用日期對象內部發現方法的參考比較。

    Ex。 在這種情況下unsortedDevices處於TablView中使用的標準ObservableList。

    SortedList<HistoryDisplayItem> sortedItems = new SortedList<>(unsortedDevices, 
          comparingInt(HistoryDisplayItem::getNumber));