排序列表视图与阵列适配器 [英] Sort listview with array adapter

查看:183
本文介绍了排序列表视图与阵列适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有连接到一个自定义的数组适配器列表视图。该列表显示了该改变的数据集的TCP连接接收到的信息......

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

我可以用列表视图排序排序(比较&LT ;?超级T>比较),但是当数据集发生变化时,列表视图是没有更多的排序..

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...

我可以使用的sort()每一个数据集被改变的时间,但我觉得这是不是最好的选择...

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?

修改

我有落实解决presented ...

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

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(新MyComparatorB(sortType)); 被称为和MyComparatorB的构造函数被调用了。但是,该方法比较(DeviceB LHS,DeviceB右)从不叫我的ArrayList是没有排序... 我做错了什么?

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?

推荐答案

我想你需要重写 notifyDataSetChanged 方式在你的适配器和执行排序之前调用其右的的。请看下图:

I guess you need to override notifyDataSetChanged method in your adapter and perform sorting right before calling its super. See below:

@Override
public void notifyDataSetChanged() {
    //do your sorting here

    super.notifyDataSetChanged();
}

这样做将会对列表进行排序,只要你叫 notifyDataSetChanged 方法来刷新列表项。否则,喂的排序列表/数组适配器。

Doing so will sort the list whenever you call notifyDataSetChanged method to refresh list items. Otherwise, feed a sorted List/array to your adapter.

或者更preferably,使用<一个href="http://developer.android.com/reference/android/widget/ArrayAdapter.html#sort%28java.util.Comparator%3C?%20super%20T%3E%29"><$c$c>sort把工作做在适配器有效的方法。

Or more preferably, use the sort method available in your adapter to get the job done.

adapter.sort(new Comparator<String>() {
    @Override
    public int compare(String lhs, String rhs) {
        return lhs.compareTo(rhs);   //or whatever your sorting algorithm
    }
});

这篇关于排序列表视图与阵列适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆