我有一个自定义的 ArrayAdapter。当数据显示在 ListViews 中时,我总是希望它排序,因此我尝试覆盖 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