Java 我想用price对我的列表视图进行排序,但是我的Web服务得到的price对象?该对象的值也类似于$49.55“;同「$39.55“;

Java 我想用price对我的列表视图进行排序,但是我的Web服务得到的price对象?该对象的值也类似于$49.55“;同「$39.55“;,java,android,sorting,listview,Java,Android,Sorting,Listview,那么我如何比较这两个值呢? 它们使用哪种数据类型? 我得到字符串类型的数据。 如何解析比较值的数据 这是我的微调器单击事件。如何使用“JSON”服务对象对上限值进行排序 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { final TextView selectedText = (TextView) parent.getC

那么我如何比较这两个值呢? 它们使用哪种数据类型? 我得到字符串类型的数据。 如何解析比较值的数据

这是我的微调器单击事件。如何使用“JSON”服务对象对上限值进行排序

        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
             final TextView selectedText = (TextView) parent.getChildAt(0);
            if (selectedText != null) {
                selectedText.setTextColor(Color.WHITE);
                if (id == 1) {
                    selectedText.setTextColor(Color.WHITE);
                    Collections.sort(mlistElectricity, new Comparator<RetailerPlanBean>(){
                        @Override
                        public int compare(RetailerPlanBean emp1, RetailerPlanBean emp2) {
                            return emp1.getmPRICE().compareToIgnoreCase(emp2.getmPRICE());
                        }
                    });
                    mRateLv = (ListView) rootview.findViewById(R.id.find_enery);
                    mRateLv.setAdapter(new FindRateAdapter(getActivity(), mlistElectricity));
                    mAllListCountTv.setText(""+mlistElectricity.size()+" LIST");
                    adapter.notifyDataSetChanged();
                }else if (id == 2){
                    selectedText.setTextColor(Color.WHITE);
                    Collections.sort(mlistElectricity, new Comparator<RetailerPlanBean>(){
                        @Override
                        public int compare(RetailerPlanBean emp1, RetailerPlanBean emp2) {
                            return emp2.getmPRICE().compareToIgnoreCase(emp1.getmPRICE());
                        }
                    });
                    mRateLv = (ListView) rootview.findViewById(R.id.find_enery);
                    mRateLv.setAdapter(new FindRateAdapter(getActivity(), mlistElectricity));
                    mAllListCountTv.setText(""+mlistElectricity.size()+" LIST");
                    adapter.notifyDataSetChanged();
                }else {
                    selectedText.setTextColor(Color.WHITE);
                }

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
public void已选择(AdapterView父视图、视图视图、int位置、长id){
最终文本视图选择了文本=(文本视图)parent.getChildAt(0);
如果(selectedText!=null){
选择text.setTextColor(Color.WHITE);
如果(id==1){
选择text.setTextColor(Color.WHITE);
Collections.sort(mlistlectricity,newcomparator(){
@凌驾
公共整数比较(RetailerPlanBean emp1,RetailerPlanBean emp2){
返回emp1.getmPRICE();
}
});
mRateLv=(ListView)rootview.findviewbyd(R.id.find_enery);
setAdapter(新的FindRateAdapter(getActivity(),mlistElectricity));
mAllListCountTv.setText(“+mlistlectricity.size()+”列表”);
adapter.notifyDataSetChanged();
}else if(id==2){
选择text.setTextColor(Color.WHITE);
Collections.sort(mlistlectricity,newcomparator(){
@凌驾
公共整数比较(RetailerPlanBean emp1,RetailerPlanBean emp2){
返回emp2.getmPRICE();
}
});
mRateLv=(ListView)rootview.findviewbyd(R.id.find_enery);
setAdapter(新的FindRateAdapter(getActivity(),mlistElectricity));
mAllListCountTv.setText(“+mlistlectricity.size()+”列表”);
adapter.notifyDataSetChanged();
}否则{
选择text.setTextColor(Color.WHITE);
}
}
}
@凌驾
未选择公共无效(AdapterView父级){
}
});

正如布拉德利提到的,您必须从字符串中提取价格

Collections.sort(mlistElectricity, new Comparator<RetailerPlanBean>(){
    @Override
    public int compare(RetailerPlanBean emp1, RetailerPlanBean emp2) {

        double emp2Price = Double.parseDouble(emp2.getmPRICE().replace("$",");
        double emp1Price = Double.parseDouble(emp1.getmPRICE().replace("$",");

        if (emp2Price == emp1Price )
            return 0;
        else if (emp1Price > emp2Price )
            return 1;
        else
            return -1;
    }
});

如果要将其作为字符串获取,请在美元符号子串上,将字符串转换为float/double(parseFloat()/parseDouble()),然后根据需要比较值或操纵数字,然后再转换回字符串(String.valueOf())以美元符号连接显示,但在我的服务中,doller还有加元和许多字母随对象一起显示,以及如何删除第一个字符。请检查我的更新答案。然而,你们会很容易在古老的SO问题中找到这些答案。请在询问之前进行研发。另外,伙计,我不能在双数据类型中使用compareToIgnoreCase。抱歉@kunu。我看不到通知。是的,因为它是双倍值,您可以在方法中使用手动比较。检查我的最新答案
double emp2Price = Double.parseDouble(emp2.getmPRICE().substring(1, emp2.getmPRICE().length());
double emp1Price = Double.parseDouble(emp1.getmPRICE().substring(1, emp1.getmPRICE().length());