18

ArrayListsを格納するものがDateあり、降順で並べ替えました。今、私はそれらをで表示したいと思いますListView。これは私がこれまでにしたことです:

 spndata.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                switch (position) {
                case 0:
                    list = DBAdpter.requestUserData(assosiatetoken);
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).lastModifiedDate != null) {
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;
                case 1:
                    list = DBAdpter.requestUserData(assosiatetoken);

                    Calendar c = Calendar.getInstance();
                    SimpleDateFormat df3 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");    
                    String formattedDate3 = df3.format(c.getTime());                        
                    Log.v("log_tag", "Date  " + formattedDate3);

                    for (int i = 0; i < list.size(); i++) {                         
                        if (list.get(i).submitDate != null) {                               
                            String sDate = list.get(i).submitDate;                              
                            SimpleDateFormat df4 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");    
                            String formattedDate4 = df4.format(sDate);

                            Map<Date, Integer> dateMap = new TreeMap<Date, Integer>(new Comparator<Date>(){  
                                 public int compare(Date formattedDate3, Date formattedDate4) {
                                     return formattedDate3.compareTo(formattedDate4);
                                 }
                            });
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;
                case 2:

                    break;

                case 3:

                    break;

                default:
                    break;
                }

            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }

        });
4

6 に答える 6

32

Arraylist<Date>Date クラスの作成。Collections.sort()昇順で 使用します。

sort(List<T> リスト)を参照

要素の自然順序付けに従って、指定されたリストを昇順に並べ替えます。

降順で並べ替えるにはCollections.reverseOrder()を参照してください

Collections.sort(yourList, Collections.reverseOrder());
于 2012-10-29T11:09:13.440 に答える
20

ケース1の場合はこのように追加してください:このように

 case 0:
     list = DBAdpter.requestUserData(assosiatetoken);
     Collections.sort(list, byDate);
     for (int i = 0; i < list.size(); i++) {
         if (list.get(i).lastModifiedDate != null) {
             lv.setAdapter(new MyListAdapter(
                     getApplicationContext(), list));
         }
     }
     break;

このメソッドをクラスの最後に配置します

static final Comparator<All_Request_data_dto> byDate = new Comparator<All_Request_data_dto>() {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

    public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) {
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = sdf.parse(ord1.lastModifiedDate);
            d2 = sdf.parse(ord2.lastModifiedDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return (d1.getTime() > d2.getTime() ? -1 : 1);     //descending
    //  return (d1.getTime() > d2.getTime() ? 1 : -1);     //ascending
    }
};
于 2012-10-29T11:51:40.047 に答える
16

使用している Date の compareTo() は、昇順で機能します。

降順を行うには、compareTo() の値を逆にするだけです。並べ替え順序を識別するコンストラクターでフラグ/列挙型を受け取る単一の Comparator クラスを使用できます

public int compare(MyObject lhs, MyObject rhs) {

    if(SortDirection.Ascending == m_sortDirection) {
        return lhs.MyDateTime.compareTo(rhs.MyDateTime);
    }

    return rhs.MyDateTime.compareTo(lhs.MyDateTime);
}

リストを実際にソートするにはCollections.sort()を呼び出す必要があります。

補足として、 for ループ内でマップを定義している理由がわかりません。コードが何をしようとしているのか正確にはわかりませんが、 for ループからマップにインデックス付きの値を入力したいと思います。

于 2012-10-29T11:06:05.030 に答える
10

文字列形式の日付を各オブジェクトの日付形式に変換する場合:

String argmodifiledDate = "2014-04-06 22:26:15";
SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            try
            {
                this.modifiledDate = format.parse(argmodifiledDate);
            }
            catch (ParseException e)
            {

                e.printStackTrace();
            }

次に、配列リストを降順で並べ替えます。

ArrayList<Document> lstDocument= this.objDocument.getArlstDocuments();
        Collections.sort(lstDocument, new Comparator<Document>() {
              public int compare(Document o1, Document o2) {
                  if (o1.getModifiledDate() == null || o2.getModifiledDate() == null)
                    return 0;     
                  return o2.getModifiledDate().compareTo(o1.getModifiledDate());
              }
            });
于 2015-02-26T07:15:11.380 に答える
4

DateComparableのリストを作成し、List<Date>を使用してソートするだけですCollections.sort()Collections.reverseOrder()でコンパレータを取得するために使用しますreverse ordering

Java ドキュメントから

指定されたコンパレータの逆順を強制するコンパレータを返します。指定されたコンパレーターが null の場合、このメソッドは reverseOrder() と同等です (つまり、 Comparable インターフェースを実装するオブジェクトのコレクションに自然順序付けの逆を強制するコンパレーターを返します)。

于 2012-10-29T11:07:35.060 に答える