【问题标题】:Compare method is not called when sorting a listview排序列表视图时不调用比较方法
【发布时间】:2012-04-13 05:18:46
【问题描述】:

我有一个自定义的 ArrayAdapter,在某些时候我调用了super.sort(new MyComparatorB(orderType, context));,但从未调用在 MyComparatorB 中实现的 compare(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();
}
//...
}

我的活动

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

【问题讨论】:

  • 你能给我们更多的信息吗?我想看看一些代码。

标签: java android comparator


【解决方案1】:

疑难解答:

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

如果您实际上没有覆盖 Comparator

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

【讨论】:

    猜你喜欢
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多