Android Studiosort()
在addAll()
方法中的行上给我一个“在线记录API的使用,自@ 1.8起”错误。我不太清楚这意味着什么...
我只想做一个自定义项List
,可以List
根据的publishedAt
属性对其进行排序Articles_Map
。
摘要文章列表:
public class AbstractArticlesList extends AbstractList<Articles_Map> {
private final List<Articles_Map> l;
public AbstractArticlesList() {
l = new ArrayList<>();
}
public Articles_Map get(int index) {
return l.get(index);
}
public Articles_Map set(int index, Articles_Map element) {
Articles_Map oldValue = l.get(index);
l.add(index, element);
return oldValue;
}
public int size() {
return l.size();
}
private void doAdd(Articles_Map another) {
l.add(another);
}
public void addAll(List<Articles_Map> others) {
for (Articles_Map a : others) {
doAdd(a);
}
l.sort(byPublishedAtComparator);
}
private final Comparator<Articles_Map> byPublishedAtComparator =
new Comparator<Articles_Map>() {
@Override
public int compare(Articles_Map o1, Articles_Map o2) {
if (o1.publishedAt == null) {
return (o2.publishedAt == null) ? 0 : -1;
} else if (o2.publishedAt == null) {
return 1;
}
return o1.publishedAt.compareTo(o2.publishedAt);
}
};
}
Articles_Map:
public class Articles_Map {
public final String title;
public final String description;
public final String url;
public final String urlToImage;
public final 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;
}
}
sort()
是API级别24(Android 7.0)的新增功能,这是由Android提供的,开始采用Java 1.8。
使用Collections.sort()
如果你minSdkVersion
低于24。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句