I'm trying to sort each entry in my NewsAdapter
's array by the publishedAt
variable found on the Articles_Map
object. I need it to be sorted by publishedAt
after each time I add a new entry using the addAll
method. Here is my code:
ListNewsActivity:
public class ListNewsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
/* ... */
// parameters for Sources endpoint
String category = "sport";
String language = "en";
String country = "us";
// Sources endpoint
Sources_Interface client_sources = NewsAPI_Adapter.createService(Sources_Interface.class);
Call<Sources_Map> call_sources = client_sources.getData(category, language, country);
call_sources.enqueue(new Callback<Sources_Map>() {
@Override
public void onResponse(Call<Sources_Map> call_sources, Response<Sources_Map> response) {
if (response.body() != null) {
final NewsAdapter nAdapter = new NewsAdapter(ListNewsActivity.this,
R.layout.article_layout);
for (final Sources_Content source : response.body().sources) {
if (source.sortBysAvailable.contains("latest")) {
// Articles endpoint
NewsAPI_Interface client = NewsAPI_Adapter.createService(NewsAPI_Interface.class);
Call<NewsAPI_Map> call = client.getData(source.id, "17f8ddef543c4c81a9df2beb60c2a478");
call.enqueue(new Callback<NewsAPI_Map>() {
@Override
public void onResponse(Call<NewsAPI_Map> call, Response<NewsAPI_Map> response) {
if (response.body() != null) {
ExpandableHeightGridView gv_content = (ExpandableHeightGridView) findViewById(R.id.gv_content);
nAdapter.addAll(response.body().articles);
gv_content.setAdapter(nAdapter);
gv_content.setExpanded(true);
}
}
@Override
public void onFailure(Call<NewsAPI_Map> call, Throwable t) {
System.out.println("An error ocurred!\n" +
"URL: " + call.request().url() + "\n" +
"Cause: " + t.getCause().toString());
}
});
}
}
}
}
@Override
public void onFailure(Call<Sources_Map> call_sources, Throwable t) {
System.out.println("An error ocurred!");
}
});
}
}
NewsAdapter:
public class NewsAdapter extends ArrayAdapter<Articles_Map> {
Context mContext;
public NewsAdapter(Context c, int resource) {
super(c, resource);
this.mContext = c;
}
public NewsAdapter(Context c, int resource, List<Articles_Map> articles) {
super(c, resource, articles);
this.mContext = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the property we are displaying
Articles_Map article = getItem(position);
// get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.article_layout, null);
ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
TextView title = (TextView) view.findViewById(R.id.title);
TextView description = (TextView) view.findViewById(R.id.description);
Picasso.with(mContext).load(article.urlToImage).into(thumbnail);
title.setText(article.title);
description.setText(article.description);
return view;
}
}
Articles_Map:
public class Articles_Map {
String title;
String description;
String url;
String urlToImage;
Date publishedAt;
public Articles_Map(String title, String description, String url, String urlToImage, Date publishedAt) {
this.title = title;
this.description = description;
this.url = url;
this.urlToImage = urlToImage;
this.publishedAt = publishedAt;
}
}
EDIT - implemented byPublishedAtComparator
's code:
private static final Comparator<Articles_Map> byPublishedAtComparator =
new Comparator<Articles_Map>() {
@Override
public int compare(Articles_Map o1, Articles_Map o2) {
if (o1.publishedAt == null || o2.publishedAt == null) {
return 0;
}
return o1.publishedAt.compareTo(o2.publishedAt);
}
};