在Android中实施排序

时间:2013-10-09 05:52:42

标签: android android-layout android-inflate

我想点击按钮对数据进行排序。

我正在解析XML并在此类的帮助下将其值存储在List中

  public class MyTripRespone {
    private List<MyTripCorporateBookingApprovals> coroprateBookingDetails = null;

    public List<MyTripCorporateBookingApprovals> getCoroprateBookingDetails() {
        return coroprateBookingDetails;
    }

    public void setCoroprateBookingDetails(
            List<MyTripCorporateBookingApprovals> coroprateBookingDetails) {
        this.coroprateBookingDetails = coroprateBookingDetails;
    }

}

在Approved Class中,我有3个值为date.id,name。

 public class MyTripCorporateBookingApprovals {

    private String insertTime ="";
    private String txid ="";
    private String journeyDate = "";
    private String passengerFirstName = "";


    public String getInsertTime() {
        return insertTime;
    }
    public void setInsertTime(String insertTime) {
        this.insertTime = insertTime;
    }
    public String getTxid() {
        return txid;
    }
    public void setTxid(String txid) {
        this.txid = txid;
    }

    public String getJourneyDate() {
        return journeyDate;
    }
    public void setJourneyDate(String journeyDate) {
        this.journeyDate = journeyDate;
    }
    public String getReturnDate() {
        return returnDate;
    }
    public void setReturnDate(String returnDate) {
        this.returnDate = returnDate;
    }
    public String getPassengerTitle() {
        return passengerTitle;
    }
    public void setPassengerTitle(String passengerTitle) {
        this.passengerTitle = passengerTitle;
    }
    public String getPassengerFirstName() {
        return passengerFirstName;
    }
    public void setPassengerFirstName(String passengerFirstName) {
        this.passengerFirstName = passengerFirstName;
    }




}

同样,对于这个名称和id,我将值放在我在循环中动态膨胀的布局中。所以当用户点击sortByDate时我想要。这些值按日期按降序排列。首先表示最新日期,然后等等。我们应该怎么做?请帮忙。

kalyan 建议之后

public class MyTripComparator implements Comparator<MyTripCorporateBookingApprovals>{

    @Override
    public int compare(MyTripCorporateBookingApprovals lhs,
            MyTripCorporateBookingApprovals rhs) {
        return lhs.getJourneyDate().compareTo(rhs.getJourneyDate());
    }



}

public void sortByDate(View v) {
    Collections.sort(//what i have to here, new MyTripComparator());

    }

1 个答案:

答案 0 :(得分:0)

试试这个

MyTripCorporateBookingApprovals类

public class MyTripCorporateBookingApprovals implements
        Comparator<MyTripCorporateBookingApprovals>,
        Comparable<MyTripCorporateBookingApprovals> {

        }

  @Override
    public int compare(MyTripCorporateBookingApprovals lhs,
            MyTripCorporateBookingApprovals rhs) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(lhs.getJourneyDate())
                    .compareTo(
                            new SimpleDateFormat("yyyy-MM-dd").parse(rhs
                                    .getJourneyDate()));
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }

@Override
    public int compareTo(MyTripCorporateBookingApprovals another) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd")
                    .parse(this.getJourneyDate()).compareTo(
                            new SimpleDateFormat("yyyy-MM-dd").parse(another
                                    .getJourneyDate()));
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }

在主要课程中,您必须使用此

Collections.sort(tripParseData.getCoroprateBookingDetails());