【问题标题】:How to sort a listview by string numbers?如何按字符串编号对列表视图进行排序?
【发布时间】:2016-04-27 12:37:18
【问题描述】:

这是我的主要问题,经过一些研究,我没有找到解决方案,所以......我想对我的自定义对象列表进行排序。这些项目有价格,但出于某种原因,它们是字符串而不是 int。我想知道如何实现这一点,感谢您的帮助!

个人小问题,对listview和recyclerview进行排序是一样的吗?

编辑:

public class Product implements Parcelable {
    private String imgUrl, titre, description, prix, nomAgence, pays, ville, type_produit, nbPieces = null;
    List<String> urlImageList_thumb = new ArrayList<>();
    List<String> urlImageList_full = new ArrayList<>();
    private int isAdded = 0;

/* getters and setters*/
}

编辑 2:在您的帮助下,这是我的可比较代码

@Override
public int compareTo(Product otherProduct) {
    String tmp = prix.replace(" €", "");
    String tmp2 = otherProduct.prix.replace(" €", "");

    //Integer p1 = Integer.valueOf(tmp);  --> does not work
    //Integer p2 = Integer.valueOf(tmp2); --> does not work

    Integer p1 = Integer.parseInt(tmp); //same error
    Integer p2 = Integer.parseInt(tmp2); // same error

    return p1.compareTo(p2);
}

这是活动中的代码:

 bouton_tri.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Collections.sort(productList);
        }
    });

编辑 3:

@Override
public int compareTo(Product otherProduct) {
    String tmp = prix.replace(" €", "").replaceAll(" ", "");
    String tmp2 = otherProduct.prix.replace(" €", "").replaceAll(" ", "");
    Integer p1 = Integer.valueOf(tmp);
    Integer p2 = Integer.valueOf(tmp2);
    return p1.compareTo(p2);
}

我仍然有一个错误,但是当我删除“€”时,如果只有空格“5300000€”,则值为“5 300 000”。但是将两者放在一起会给我这个错误 java.lang.NumberFormatException: Invalid int: "-" ... 有什么想法吗?谢谢

【问题讨论】:

  • 你试过 Collections.sort 吗?
  • 分享示例数据。
  • @PoojaGaikwad 正在写入。
  • @P.Rai 添加了代码 :)
  • 您要对哪个列表进行排序?请看我的回答

标签: android sorting listview


【解决方案1】:

您可以修改您的Product 类以实现 Comparable

在将String 转换为Integer 之前,您需要删除 和所有空格。

public class Product implements Parcelable, Comparable<Product> {
    private String prix;

    //...

    @Override
    public int compareTo(Product otherProduct) {
        String tmp = prix.replace(" €", "").replaceAll(" ", "");
        String tmp2 = otherProduct.prix.replace(" €", "").replaceAll(" ", "");
        Integer p1 = Integer.valueOf(tmp);
        Integer p2 = Integer.valueOf(tmp2);
        return p1.compareTo(p2);
    }
}

对您的集合进行排序后,您可以使用:Collections.sort(...); 此方法会将您在适配器中使用的自定义对象列表作为参数。

例如:

List<Product> l = new ArrayList();
Collections.sort(l);

请注意,对集合进行排序不会刷新recyclerview 的视图。

你必须在你的适配器上调用notifyDataSetChanged() 来刷新recyclerview:

您可以在声明适配器的主要活动中执行此操作:

yourAdapter.notifyDataSetChanged();

【讨论】:

  • 谢谢你的回答,我认为它有效,我唯一的问题是我没有注意到字符串中的货币符号,所以我有一个无效int的错误,这是正常的...我认为它有效,所以我会接受你的回答
  • 我想问如何取消排序?我想使用一个按钮对列表进行排序/取消排序,谢谢@Guillaume!
  • 看看Collections.shuffle(...),你需要...docs.oracle.com/javase/6/docs/api/java/util/…
  • 我找到了去掉货币符号的方法,但出现了这个错误:java.lang.NumberFormatException: Invalid int: "5 300 000"
  • String tmp = prix.replaceAll(" ", "");字符串 tmp2 = otherProduct.prix.replaceAll(" ", ""); tmp = tmp.replace("€", ""); tmp2 = tmp.replace("€", "");我添加了这个但它不起作用,java.lang.NumberFormatException: Invalid int: "-" 否则要取消空格它可以工作
【解决方案2】:

假设你有 List&lt;String&gt; sampleData 对象

Collections.sort(sampleData, new Comparator<String>() {
        @Override
        public int compare(String c1, String c2) {
            return Integer.valueOf(c1) -  Integer.valueOf(c2);
        }
    });

这将对您的数据进行排序。

【讨论】:

    【解决方案3】:
    (int) Integer.parseInt(p2.getNumberOfRecords()) - Integer.parseInt(p1.getNumberOfRecords())
    

    因此,String 数据类型中整数的简单比较不会正确结果,而是首先通过以下方式解析字符串:

    int value = Integer.parseInt(string)
    

    【讨论】:

    • 我将您的解决方案与@Guillaume 的答案混合使用,但也不起作用,仍然有 java.lang.NumberFormatException: Invalid int: "5 300 000"
    【解决方案4】:

    试试这个:

    Collections.sort (list, new Comparator<String> () {
    
        @Override
        public int compare (String s1, String s2) {
    
            return s1.compareToIgnoreCase(s2);
        }
    });
    
    OR
    
    
    Collections.sort (list, new Comparator<String> () {
    
        @Override
        public int compare (String s1, String s2) {
    
            //cast string price to integer
            int price1 = Integer.parseInt(s1);
            int price2 = Integer.parseInt(s2);
            if (price1 > price1) {
    
                return 1;
            }
            else if (price2 > price1) {
    
                return -1;
            }
            else {
    
                return 0;
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 2014-04-15
      • 2020-06-13
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多