排序列表视图时不调用Compare方法

时间:2012-03-29 14:29:40

标签: java android comparator

我有一个自定义的ArrayAdapter,在某些时候我调用super.sort(new MyComparatorB(orderType, context));但是从未调用MyComparatorB中实现的比较(DeviceB lhs,DeviceB rhs)方法,并且下一条指令正常执行...可能是什么问题?

更新

MyComparatorB

public class MyComparatorB implements Comparator<DeviceB> {

private String orderType;
private Context cont;

public MyComparatorB(String type, Context cont) {

    this.orderType = type;
    this.cont = cont;


}

public int compare(DeviceB lhs, DeviceB rhs) {

    int res = 0;
    if (orderType.equals(cont.getString(R.string.alpha_text))) {
        res = (lhs.getName()).compareTo(rhs.getName());
    }
    else if (orderType.equals(cont.getString(R.string.lastAct_text))) {
        res = (rhs.getTime().getTime()).compareTo(lhs.getTime().getTime());
    }
    return res;
}

}

DeviceBAdapter

public class DeviceBAdapter extends ArrayAdapter<DeviceB> implements Filterable {

private Context context;
private ArrayList<DeviceB> allDev;
private ArrayList<DeviceB> dev;
//...

public DeviceBAdapter(Context context, BliveAPI blapi) {
    super(context, R.layout.sensor_row_state_list);
    this.context = context;
    this.bliveapi = blapi;
    this.allDev = allDevToArrayList();
    this.dev = new ArrayList<DeviceB>(allDev);
    notifyDataSetChanged();
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}


public void sort(String s) {

    super.sort(new MyComparatorB(s,context));
    notifyDataSetChanged();
}
//...
}

MyActivity

//...
myDevAdapter.sort(getString(R.string.alpha_text));
//...

1 个答案:

答案 0 :(得分:0)

故障排除:

尝试在@Override

之前添加public int compare(DeviceB lhs, DeviceB rhs)

如果你没有覆盖 Comparator

中的比较方法,这会引发警告/错误