I don't know How to sort my ArrayList adapter by date in reverse order
I think my code is fine but my list don't show my array in reversed sort. I tried every way and see many question but I didn't managed to achieve my goal.
Here's my code:
in G class :
public static ArrayList<StructureImage> images = new ArrayList<StructureImage>();
and :
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());
}
}
and fill method :
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);
}
and i use image array :
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();
}
}
}