我有一个自定义的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;
}

}


DeviceB适配器

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();
}
//...
}


我的活动

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

最佳答案

故障排除:

尝试在@Override之前添加public int compare(DeviceB lhs, DeviceB rhs)

如果您实际上并未覆盖Comparator中的compare方法,则应该引发警告/错误

09-30 17:52