android arrayadapter排序,android - Sort listview with array adapter - Stack Overflow

I have a listview connected to a custom array adapter. This list shows information received by a TCP connection which changes the dataSet...

I am able to sort the listview with sort (Comparator super T> comparator), but when the dataSet is changed, the listview is no more sorted...

I can use sort () every time the dataSet is changed, but I think this is not the best option...

How can I do that? Any suggestions?

EDIT

I am having problems in implementing the solutions presented...

MyComparatorB.java

public class MyComparatorB implements Comparator {

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;

}

}

Snippet of my 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();

}

In my MyActivity.java

myDevAdapter.sort(SortType.ALPHA);

When I am debugging, the method super.sort(new MyComparatorB(sortType)); is called and the constructor of MyComparatorB is called too. But the method compare(DeviceB lhs, DeviceB rhs) is never called and my arrayList is not sorted...

What I am doing wrong?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值