cri*_*024 11 java android date arraylist sortedlist
我有一个ArrayList
存储Date
s,我按降序排序.现在我想在一个显示它们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) {
}
});
Run Code Online (Sandbox Code Playgroud)
Sum*_*ngh 18
创建Arraylist<Date>
Date类.并Collections.sort()
用于升序.
根据元素的自然顺序,将指定列表按升序排序.
对于按降序排序请参阅Collections.reverseOrder()
Collections.sort(yourList, Collections.reverseOrder());
Run Code Online (Sandbox Code Playgroud)
Jig*_*iya 17
在案例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;
Run Code Online (Sandbox Code Playgroud)
并将此方法放在您班级的最后
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
}
};
Run Code Online (Sandbox Code Playgroud)
Ste*_*bbi 10
您正在使用的日期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);
}
Run Code Online (Sandbox Code Playgroud)
您需要调用Collections.sort()来实际排序列表.
作为旁注,我不确定为什么要在for循环中定义地图.我不确定您的代码尝试做什么,但我假设您想要将for循环中的索引值填充到地图中.
如果字符串格式的日期将其转换为每个对象的日期格式:
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();
}
Run Code Online (Sandbox Code Playgroud)
然后按降序对arraylist进行排序:
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());
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
83062 次 |
最近记录: |