我有一个自定义的ArrayAdapter。当数据在ListView中显示时,我总是希望它被排序,因此我尝试重写notifyDataSetChanged()方法来先对适配器进行排序,像这样:
@Override
public void notifyDataSetChanged() {
sort(new Comparator<IceContact>() {
@Override
public int compare(IceContact iceContact, IceContact iceContact2) {
return iceContact.compareTo(iceContact2);
}
});
super.notifyDataSetChanged();
}
IceContact类是一个实现Comparable接口的get-set类。不幸的是,这给我带来了一个讽刺的StackOverflowError(如下面logcat所示)。然后我尝试反过来做。覆盖sort()
并在此之后直接调用notifyDataSetChanged()
,代码如下:
@Override
public void sort(Comparator<? super IceContact> comparator) {
super.sort(comparator);
notifyDataSetChanged();
}
这很好!有人能解释一下吗?感觉它们的实现方式基本相同。
01-21 18:25:50.725: ERROR/AndroidRuntime(2490): FATAL EXCEPTION: main
java.lang.StackOverflowError
at android.view.View.setFocusableInTouchMode(View.java:5584)
at android.widget.AdapterView.checkFocus(AdapterView.java:717)
at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:812)
at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6044)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:70)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(IceContactAdapter.java:71)
at android.widget.ArrayAdapter.sort(ArrayAdapter.java:278)
at se.naxiait.curbits.utils.IceContactAdapter.notifyDataSetChanged(Ic
sort()
的新函数(没有参数),这样每次想要对数据进行排序时就不必传递比较器了。 - Niklas Ekman