【发布时间】:2012-04-11 23:36:14
【问题描述】:
我有一个连接到自定义数组适配器的列表视图。此列表显示 TCP 连接接收到的更改数据集的信息...
我可以使用sort (Comparator<? super T> comparator) 对列表视图进行排序,但是当数据集更改时,列表视图不再排序...
我可以在每次更改数据集时使用sort (),但我认为这不是最好的选择...
我该怎么做?有什么建议吗?
编辑
我在实施所提供的解决方案时遇到问题...
MyComparatorB.java
public class MyComparatorB implements Comparator<DeviceB> {
private byte orderType;
public MyComparatorB(byte type) {
this.orderType = type;
}
public int compare(DeviceB lhs, DeviceB rhs) {
int res = 0;
if (orderType == SortType.ALPHA) {
res = (lhs.getName()).compareTo(rhs.getName());
}
else if (orderType == SortType.LAST_ACT) {
res = (rhs.getTime().getTime()).compareTo(lhs.getTime().getTime());
}
return res;
}
}
我的 customArrayAdapter.java 的片段
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
//-----------Order the content of the arrayAdapter------------//
public void sort(byte sortType) {
super.sort(new MyComparatorB(sortType));
notifyDataSetChanged();
}
在我的 MyActivity.java 中
myDevAdapter.sort(SortType.ALPHA);
当我调试时,方法super.sort(new MyComparatorB(sortType));被调用,MyComparatorB的构造函数也被调用。但是方法compare(DeviceB lhs, DeviceB rhs) 从未被调用,并且我的arrayList 没有排序......
我做错了什么?
【问题讨论】:
-
为什么不使用ArrayList对数组排序和恢复数组列表!
-
我认为你应该对数组进行排序并调用 adapter.notifyDataSetChanged()
-
@amp : 这里的 SortType 是什么?
-
@YuDroid:这只是一个我定义可能的排序类型的类。喜欢:
public class SortType{ public final static int ALPHA= 1; public final static int LAST_ACT= 2;}