Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何按字符串编号对listview排序?_Android_Sorting_Listview - Fatal编程技术网

Android 如何按字符串编号对listview排序?

Android 如何按字符串编号对listview排序?,android,sorting,listview,Android,Sorting,Listview,这是我的主要问题,经过一些研究,我没有找到解决办法,所以。。。我想对自定义对象列表进行排序。这些项目有一个价格,但出于一个原因,他们是字符串不是整数。我想知道如何实现这一点,谢谢你的帮助 个人小问题,对listview和recyclerview进行排序,它们是以同样的方式进行的吗 编辑: 以下是活动中的代码: bouton_tri.setOnClickListener(new View.OnClickListener() { public void onClick(View v

这是我的主要问题,经过一些研究,我没有找到解决办法,所以。。。我想对自定义对象列表进行排序。这些项目有一个价格,但出于一个原因,他们是字符串不是整数。我想知道如何实现这一点,谢谢你的帮助

个人小问题,对listview和recyclerview进行排序,它们是以同样的方式进行的吗

编辑:

以下是活动中的代码:

 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);
}
我仍然有一个错误,但当我刚去掉“€”时,值是“530000”,如果只有空格“5300000欧元”。但将两者放在一起会产生以下错误java.lang.NumberFormatException:无效int:“-”。。。有什么想法吗?谢谢

试试这个:

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;
        }
    }
});
Collections.sort(列表,新的比较器(){
@凌驾
公共整数比较(字符串s1、字符串s2){
返回s1.compareToIgnoreCase(s2);
}
});
或
Collections.sort(列表,新比较器(){
@凌驾
公共整数比较(字符串s1、字符串s2){
//将字符串价格强制转换为整数
intprice1=Integer.parseInt(s1);
intprice2=Integer.parseInt(s2);
如果(价格1>价格1){
返回1;
}
否则如果(价格2>价格1){
返回-1;
}
否则{
返回0;
}
}
});
试试这个:

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;
        }
    }
});
Collections.sort(列表,新的比较器(){
@凌驾
公共整数比较(字符串s1、字符串s2){
返回s1.compareToIgnoreCase(s2);
}
});
或
Collections.sort(列表,新比较器(){
@凌驾
公共整数比较(字符串s1、字符串s2){
//将字符串价格强制转换为整数
intprice1=Integer.parseInt(s1);
intprice2=Integer.parseInt(s2);
如果(价格1>价格1){
返回1;
}
否则如果(价格2>价格1){
返回-1;
}
否则{
返回0;
}
}
});

您可以修改您的
产品
类以实现可比较的

在将
字符串
转换为
整数
之前,需要删除
和所有空格

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);
    }
}
请注意,对集合进行排序不会刷新
recyclerview
的视图

您必须在适配器上调用
notifyDataSetChanged()
,以刷新recyclerview:

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

yourAdapter.notifyDataSetChanged();

您可以修改
产品
类以实现可比较的

在将
字符串
转换为
整数
之前,需要删除
和所有空格

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);
    }
}
请注意,对集合进行排序不会刷新
recyclerview
的视图

您必须在适配器上调用
notifyDataSetChanged()
,以刷新recyclerview:

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

yourAdapter.notifyDataSetChanged();

假设您有
列表sampleData
对象

Collections.sort(sampleData, new Comparator<String>() {
        @Override
        public int compare(String c1, String c2) {
            return Integer.valueOf(c1) -  Integer.valueOf(c2);
        }
    });
Collections.sort(sampleData,newcomparator(){
@凌驾
公共整数比较(字符串c1、字符串c2){
返回Integer.valueOf(c1)-Integer.valueOf(c2);
}
});

这将对您的数据进行排序

假设您有
列表sampleData
对象

Collections.sort(sampleData, new Comparator<String>() {
        @Override
        public int compare(String c1, String c2) {
            return Integer.valueOf(c1) -  Integer.valueOf(c2);
        }
    });
(int) Integer.parseInt(p2.getNumberOfRecords()) - Integer.parseInt(p1.getNumberOfRecords())
Collections.sort(sampleData,newcomparator(){
@凌驾
公共整数比较(字符串c1、字符串c2){
返回Integer.valueOf(c1)-Integer.valueOf(c2);
}
});
这将对您的数据进行排序

(int) Integer.parseInt(p2.getNumberOfRecords()) - Integer.parseInt(p1.getNumberOfRecords())
因此,对字符串数据类型中的整数进行简单比较不会得到正确的结果,而是首先通过以下方式解析字符串:

int value = Integer.parseInt(string)
因此,对字符串数据类型中的整数进行简单比较不会得到正确的结果,而是首先通过以下方式解析字符串:

int value = Integer.parseInt(string)


您是否尝试过Collections.sort?共享示例数据。@PoojaGaikwad正在编写。@P.Rai添加了代码:)您想对哪个列表进行排序?请参阅我的回答您是否尝试过Collections.sort?共享示例数据。@PoojaGaikwad正在编写。@P.Rai添加了代码:)您想对哪个列表进行排序?请看我的回答谢谢你的回答,我认为它是有效的,我唯一的问题是我没有注意到字符串中的货币符号,所以我有一个无效int的错误,这是正常的。。。我认为它是有效的,所以我会接受你的回答。我想问一下如何取消列表的排序?我想使用一个按钮对列表进行排序/取消排序,谢谢@Guillaume!查看一下集合。shuffle(…)这正是您需要的…我找到了一种去除货币符号的方法,但出现了以下错误:java.lang.NumberFormatException:无效int:“530000”字符串tmp=prix.replaceAll(“,”);字符串tmp2=otherProduct.prix.replaceAll(“,”);tmp=tmp。替换(“€”和“);tmp2=tmp.替换(“€”和“);我添加了这个,但它不起作用,java.lang.NumberFormatException:Invalid int:“-”否则去掉空格它就起作用了谢谢你的回答,我认为它起作用了,唯一的问题是我没有注意到字符串中的货币符号,所以我有一个无效int的错误,这是正常的。。。我认为它是有效的,所以我会接受你的回答。我想问一下如何取消列表的排序?我想使用一个按钮对列表进行排序/取消排序,谢谢@Guillaume!查看一下集合。shuffle(…)这正是您需要的…我找到了一种去除货币符号的方法,但出现了以下错误:java.lang.NumberFormatException:无效int:“530000”字符串tmp=prix.replaceAll(“,”);字符串tmp2=otherProduct.prix.replaceAll(“,”);tmp=tmp。替换(“€”和“);tmp2=tmp.替换(“€”和“);我添加了这个,但它不起作用,java.lang.NumberFormatException:Invalid int:“-”否则要去掉它起作用的空格,我使用了你的解决方案与@Guillaume的答案混在一起,但也不起作用,仍然有java.lang.NumberFormatException:Invalid int:“530000”我使用了你的答案