我不知道如何按日期以相反的顺序对我的 ArrayList 适配器进行排序
我认为我的代码很好,但我的列表没有以反向排序显示我的数组。我尝试了各种方法,看到了很多问题,但我没有达到我的目标。
这是我的代码:
G类:
public static ArrayList<StructureImage> images = new ArrayList<StructureImage>();
和 :
public class StructureImage implements Comparable<StructureImage> {
public String title;
public String tozihat;
public String thumbnail;
public String url;
Date dateTime;
public void setDateTime(Date datetime) {
this.dateTime = datetime;
}
public Date getDateTime() {
return dateTime;
}
@Override
public int compareTo(StructureImage o) {
Collections.sort(G.images, Collections.reverseOrder());
if (getDateTime() == null || o.getDateTime() == null)
return 0;
return getDateTime().compareTo(o.getDateTime());
}
}
和填充方法:
public void fill(final ArrayAdapter<StructureImage> adapter, final StructureImage item, final int position) {
txtTitle.setText(item.title);
txtTozih.setText(item.tozihat);
imgPreview.setClickable(true);
String filename = item.thumbnail.replace("/thumbnail/", "");
intent.putExtra("filename", filename);
final File imageFile = new File(G.DIR_FINAL + "/" + filename);
if ( !imageFile.exists()) {
imgPreview.setImageBitmap(null);
DownloadManager.addToDownloadList(item.thumbnail);
}
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
imgPreview.setImageBitmap(bitmap);
}
我使用图像数组:
private void getImageListFromServer() {
Date date = new Date(0);
String result = Webservice.readUrl("http://192.168.1.100/file-server/service.php", null);
if (result != null) {
try {
images.clear();
JSONArray tasks = new JSONArray(result);
for (int i = 0; i < tasks.length(); i++) {
JSONObject object = tasks.getJSONObject(i);
StructureImage image = new StructureImage();
image.title = object.getString("title");
image.tozihat = object.getString("tozihat");
image.thumbnail = object.getString("thumbnail");
image.url = object.getString("url");
DownloadManager.addToDownloadList(image.thumbnail);
images.add(image);
}
adapter.notifyDataSetChanged();
}
catch (JSONException e) {
e.printStackTrace();
}
}
}