然后我如何比较两个值? 他们使用哪种数据类型? 我得到字符串类型的数据。 如何解析比较值的数据?
这是我的微调器点击事件。如何使用我的“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) {
}
});
答案 0 :(得分:0)
正如布拉德利所说,你必须从字符串中提取价格
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;
}
});
以上内容仅适用于&#39; $&#39;
您也可以使用subString方法并删除第一个字符。
使用subString方法
double emp2Price = Double.parseDouble(emp2.getmPRICE().substring(1, emp2.getmPRICE().length());
double emp1Price = Double.parseDouble(emp1.getmPRICE().substring(1, emp1.getmPRICE().length());