Android开发之对ListView的数据进行排序

这里涉及到对ListView的数据进行排序,以及ListView的数据如何清空处理。排序的方法相同,但是里面的数据集合有些区别:一种是利用pojo类取得数据;另一种是利用map来取得数据。

第一种:利用pojo类完成排序

pojo类:HuoLiang.class

public class HuoLiang {
private String dianmian;// 店面
private String total_price;// 运费
private String agent_cargo_charge;// 代收
private String shuliang;// 运单
private String quantity;// 件数 public String getDianmian() {
return dianmian;
} public void setDianmian(String dianmian) {
this.dianmian = dianmian;
} public String getTotal_price() {
return total_price;
} public void setTotal_price(String total_price) {
this.total_price = total_price;
} public String getAgent_cargo_charge() {
return agent_cargo_charge;
} public void setAgent_cargo_charge(String agent_cargo_charge) {
this.agent_cargo_charge = agent_cargo_charge;
} public String getShuliang() {
return shuliang;
} public void setShuliang(String shuliang) {
this.shuliang = shuliang;
} public String getQuantity() {
return quantity;
} public void setQuantity(String quantity) {
this.quantity = quantity;
} @Override
public String toString() {
return "HuoLiang [dianmian=" + dianmian + ", total_price="
+ total_price + ", agent_cargo_charge=" + agent_cargo_charge
+ ", shuliang=" + shuliang + ", quantity=" + quantity + "]";
}
    List<HuoLiang> internetlist= new ArrayList<HuoLiang>();//从服务器上加载的数据
    // 将集合中的值赋给另一个集合,前提条件是两个集合的类型一致
    List<HuoLiang> locallist = new ArrayList<HuoLiang>(internetlist);
    //listView的自定义的适配器,是本地数据集合locallist和ListView的桥梁
    HLCheckAdapter localadapter= new HLCheckAdapter(locallist, R.layout.check_item,HLCheckActivity.this);
    //服务器加载的数据集合internetlist与listView的桥梁
    HLCheckAdapter adapter= new HLCheckAdapter(internetlist, R.layout.check_item,HLCheckActivity.this);
    if (internetlist != null && internetlist.size() != 0) {
    //将本地数据集合清空,然后加载到适配器中,这样才能做到ListView的数据的清空,方便下面的排序
    locallist.clear();
    listView.setAdapter(localadapter);
    //升序
    Collections.sort(internetlist, new Comparator<HuoLiang>() { @Override
public int compare(HuoLiang t1, HuoLiang t2) {
// TODO Auto-generated method stub
if (Float.parseFloat(t1.getTotal_price()) > Float
.parseFloat(t2.getTotal_price())) {
return 1;
}
if (Float.parseFloat(t1.getTotal_price()) < Float
.parseFloat(t2.getTotal_price())) {
return -1;
}
return 0;
}
});
// 将排序后的集合加载到listView中
listView.setAdapter(adapter);
}
          //降序
          locallist.clear();
listView.setAdapter(localadapter);
Collections.sort(internetlist, new Comparator<HuoLiang>() { @Override
public int compare(HuoLiang t1, HuoLiang t2) {
// TODO Auto-generated method stub
if (Float.parseFloat(t1.getTotal_price())<Float.parseFloat(t2.getTotal_price())) {
return 1;
}
if (Float.parseFloat(t1.getTotal_price())>Float.parseFloat(t2.getTotal_price())) {
return -1;
}
return 0;
}
});
// 将排序后的集合加载到listView中
listView.setAdapter(adapter);

第二种:利用Map进行排序

        // 清空listView列表
localList.clear();
listView.setAdapter(localadapter); // 排序
Collections.sort(listitem,new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
if (Float.parseFloat(o1.get("agent_cargo_charge").toString()) > Float.parseFloat(o2.get("agent_cargo_charge").toString())) {
return 1;
} else if (Float.parseFloat(o1.get("agent_cargo_charge").toString()) < Float.parseFloat(o2.get("agent_cargo_charge").toString())) {
return -1;
}
return 0;
}
});
// 将排序后的集合加载到listView中
listView.setAdapter(hlCheckAdapter);
上一篇:Aizu 2304 Reverse Roads(无向流)


下一篇:Axios的简单用法