我有一个自定义的 ArrayAdapter,有时我调用 super.sort(new MyComparatorB(orderType, context));
但是在 MyComparatorB 中实现的 compare(DeviceB lhs, DeviceB rhs) 方法永远不会调用并正常执行下一条指令......可能是什么问题?
更新
我的比较器B
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;
}
}
设备适配器
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();
}
//...
}
我的 Activity
//...
myDevAdapter.sort(getString(R.string.alpha_text));
//...
最佳答案
疑难解答:
尝试在 public int compare(DeviceB lhs, DeviceB rhs)
之前添加 @Override
如果您实际上没有重写比较器中的比较方法,这应该会引发警告/错误
关于java - 排序 ListView 时不调用比较方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9927435/