【问题标题】:Comparator Function in Adapter on Float values适配器中浮点值的比较器函数
【发布时间】: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&lt;Profiles&gt;) method Collections&lt;T#1&gt;sort(List&lt;T#1&gt;) 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


    【解决方案1】:

    根据docs 有两种排序方法,您正在调用的sortsort(List&lt;T&gt; list) 希望您实现Comparable 而不是Comparator 它通过调用compareTo() 方法以自然顺序对元素进行排序如果他们来自像String 类的库。您调用的排序找不到该方法,因为您有 compare() 而不是 compareTo()

    带有Comparator 的是sort(List&lt;T&gt; list, Comparator&lt;? super T&gt; c) 在您的情况下,您可以像 Collections.sort(list,this) 一样使用它而不是 Collections.sort(list) 如果您只想使用 Comparator 否则您可以实现 Comparable 并覆盖 compareTo 并且它不像不会使用 Profiles 列表它需要任何它应该实现正确的接口

    我会建议像下面那样做,在这种情况下你不必实现任何接口在 java 8 或更高版本中

    Collections.sort(list,(profiles,t1) -> Float.compare(profiles.getDistance,t1.getDistance()) 
    

    低于 java 8

    Collections.sort(list,new Comparator()<Profiles>{
    @Override
    public int compare(Profiles profiles,Profiles t1){
       return Float.compare(profiles.getDistance,t1.getDistance);
      });
    

    祝你有美好的一天乌梅尔·伊克巴尔

    【讨论】:

    • 谢谢,成功了。你能详细说明一下这个 Collections.sort(list,(profiles,t1) -> Float.compare(profiles.getDistance,t1.getDistance()) 它是一个函数还是它是如何工作的,就像它如何返回可比较的值一样,如果我现在必须按降序显示值,它按升序显示
    • 我想你不知道 lambda 表达式,我正在编辑答案,然后解释一下
    • previously 是我现在编辑的简写,编译器知道第二个参数取 Compartor 所以不要提,它也知道只有一种方法可以比较所以也不要提那,它也知道该方法采用什么类型的参数,因此您也不必提及编译器唯一不知道的是后面的内容 -> 所以我们只提供它,这有点简化,所以需要了解 lambda 表达式以了解它们到底是什么以及你可以用它们做什么,它们很棒,使用它们可以使代码更简洁和可读
    • 了解更多关于他们的信息youtu.be/f4QZ12wMQO8 还可以观看其播放列表的其他视频
    • 要做降序只是做Float.compare(t1.getDistance(),profiles.getDistance())基本上只是切换参数,因为它正在做类似return profiles.getDitance &gt; t1.getDistance()的事情
    猜你喜欢
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多