2012-03-28 156 views
26

我有一个列表视图连接到自定义阵列适配器。这份清单显示通过改变DataSet中的TCP连接接收到的信息......排序列表视图与阵列适配器

我可以列表视图与sort (Comparator<? super T> comparator)排序,但是当这些数据改变时,列表视图中没有更多的分类......

每次dataSet被更改时我都可以使用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不排序... 我做错了什么?

+0

为什么你不使用ArrayList并对数组和列进行排序! – Behnam 2012-03-28 11:28:22

+0

我想你应该排序你的数组,并调用adapter.notifyDataSetChanged() – 2012-03-28 11:29:31

+0

@amp:什么是SortType在这里? – YuDroid 2012-12-11 05:43:40

回答

38

我猜你需要重写notifyDataSetChanged在适配器方法和执行排序正确调用其超级之前。请看下图:

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

    super.notifyDataSetChanged(); 
} 

这样做,只要你拨打notifyDataSetChanged方法来刷新列表项目将列表进行排序。否则,将已排序的列表/数组提供给您的适配器。


或者更优选地,使用适配器中的sort方法来完成工作。

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

每次dataSet被更改时,唯一的方法是调用'super.sort(comparator)'方法吗? – amp 2012-03-28 13:36:00

+0

是的,在调用super.notifyDataSetChanged之前添加它,以便您的数据每次都被排序 – waqaslam 2012-03-28 13:36:52

+19

.sort函数自动调用notifyDataSetChanged,因此如果您使用此代码并进行内置排序,您将进入无限循环并溢出你的堆栈。 只有在你自己管理列表的情况下才能做到这一点,但大多数使用数组适配器的人都没有这样做。 – elBradford 2013-08-28 03:30:04

0

由于适配器填充ListView,唯一的方法是以有序方式传递数据对象。

0

一旦对listView进行了排序,就可以从您的活动中调用adapter.notifyDataSetChanged()。

5
  • 数据存储到一个ArrayList
  • 附加阵列到适配器
  • 排序阵列,并分配给自身
  • 使用notifyDataSetChanged()刷新适配器
3

这对我的作品

@Override 
public void notifyDataSetChanged() { 
    this.setNotifyOnChange(false); 

    this.sort(new Comparator<String>() { 
     @Override 
     public int compare(String lhs, String rhs) { 
      return lhs.compareTo(rhs); 
     } 
    }); 

    this.setNotifyOnChange(true); 
} 
0

Per elBradford对此的评论由于notifyDataSetChanged()方法导致堆栈溢出的可能性,我最终重写了ArrayAdapter的add()方法,以确保这些项目在逐个添加时保持排序的一致性。 (当调用addAll()时,我在先排序项目;但我想你也可以重载addAll())。

@Override 
    public void add(ListThing object) { 
     super.add(object); 
     this.sort(ListThing.sComparator); 
    } 
0

基于waqaslamKristianR的回答是:

@Override 
public void notifyDataSetChanged() { 
    this.setNotifyOnChange(false); 

    this.sort(new Comparator<String>() { 

     @Override 
     public int compare(String s1, String s2) { 
      return s1.compareTo(s2); 
     } 
    }); 

    super.notifyDataSetChanged(); 
    this.setNotifyOnChange(true); 
} 

希望它能帮助。