按反向日期对适配器 ArrayList 进行排序
Sort adapter ArrayList by reverse date
我不知道如何按日期倒序排列我的 ArrayList 适配器
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.
这是我的代码:
in G class :
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();
}
}
}
您必须致电:
Collections.sort(yourListOfComparablesObjects);
下单生效
排序不会自动进行,您必须在需要排序列表时调用 order。
您的代码在 G.images 上调用排序,这是错误的,因为如果不是列表中的排序,则永远不会调用此方法。
要在反向顺序和正常顺序之间切换,您必须更改:
return getDateTime().compareTo(o.getDateTime());
至:
return o.getDateTime().compareTo(getDateTime());
您的代码:
在后面添加:
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);
}
做:
Collections.sort(images);
adapter.notifyDataSetChanged();
并删除您的比较方法中的排序调用。
..您的代码正在做一些令人讨厌的事情,您是否打算在每个排序循环内进行排序?
请继续并从您的 compareTo
函数中删除 Collections.sort(G.images, Collections.reverseOrder());
。
@Override
public int compareTo(StructureImage o) {
if (getDateTime() == null || o == null || o.getDateTime() == null)
return 0;
return getDateTime().compareTo(o.getDateTime());
}
..再试一次,你可能想在另一个地方调用 sort Collections.sort
行。
我不知道如何按日期倒序排列我的 ArrayList 适配器
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.
这是我的代码:
in G class :
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();
}
}
}
您必须致电:
Collections.sort(yourListOfComparablesObjects);
下单生效
排序不会自动进行,您必须在需要排序列表时调用 order。
您的代码在 G.images 上调用排序,这是错误的,因为如果不是列表中的排序,则永远不会调用此方法。
要在反向顺序和正常顺序之间切换,您必须更改:
return getDateTime().compareTo(o.getDateTime());
至:
return o.getDateTime().compareTo(getDateTime());
您的代码:
在后面添加:
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);
}
做:
Collections.sort(images);
adapter.notifyDataSetChanged();
并删除您的比较方法中的排序调用。
..您的代码正在做一些令人讨厌的事情,您是否打算在每个排序循环内进行排序?
请继续并从您的 compareTo
函数中删除 Collections.sort(G.images, Collections.reverseOrder());
。
@Override
public int compareTo(StructureImage o) {
if (getDateTime() == null || o == null || o.getDateTime() == null)
return 0;
return getDateTime().compareTo(o.getDateTime());
}
..再试一次,你可能想在另一个地方调用 sort Collections.sort
行。