【发布时间】:2020-06-21 10:35:25
【问题描述】:
我与数据库的距离越来越远,我想对我的数据进行排序,比如列表中最短的距离应该首先显示在 RecyclerView 中。 我已经在 m Profile 模型类中实现了比较器,如下所示:
public class Profiles implements Comparator<Profiles>
然后重写模型类中的方法:
@Override
public int compare(Profiles profiles, Profiles t1) {
return Float.compare(profiles.getDistance(), t1.getDistance());
}
现在在我设置适配器的班级中,我没有得到应该将哪个列表传递给 Collections.sort() 因为我给它提供了 Profile 类型的 List 和它给我这个错误:
no suitable method found for sort(ArrayList<Profiles>) method Collections<T#1>sort(List<T#1>) is not applicable
这就是我在 RecyclerView 课程中所做的事情
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Profiles profiles = ds.getValue(Profiles.class);
profiles.setDistance(distanceFunc());
String covid = (String) ds.child("covid_recovered").getValue();
String dengue = (String) ds.child("dengue_recovered").getValue();
for (DataSnapshot option : ds.child("matched_bloodGroups").getChildren()) {
canDonateBG.add(String.valueOf(option.getValue()));
}
if (canDonateBG.contains(bloodGroup)) {
if (recoverey != null) {
if (covid.equals(covidRecover)) {
list.add(profiles);
canDonateBG.clear();
} else if (dengue.equals(dengueRecover)) {
list.add(profiles);
canDonateBG.clear();
}
}
Collections.sort(list);
donarAdapter = new DonarAdapter(DonarList.this, list);
recyclerView.setAdapter(donarAdapter);
}
那么我应该将哪个列表传递给排序函数,因为它不接受 Profile 类型的列表
【问题讨论】:
标签: android arraylist android-recyclerview comparator