我有一个自定义ArrayAdapter.当ListVIEws中显示数据时,我总是希望它排序,因此我尝试覆盖notifyDataSetChanged()以首先对适配器进行排序,如下所示:
@OverrIDepublic 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(),如下所示:
@OverrIDepublic 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
解决方法:
The class IceContact is a get-set class that implement Comparable.
Unfortunately this is giving me a, ironically, StackOverflowError
(logcat below).
sort()调用是默认的(在它的实现中)notifyDataSetChanged()然后再调用sort(),然后调用notifyDataSetChanged(),它将调用sort(),它将调用…
在notifyDataSetChanged排序中,不调用notifyDataSetChanged()作为sort()的数据将自动为您执行此 *** 作.
第二种情况将起作用,因为你基本上会调用notifyDataSetChanged()两次(super.sort(),从你的super call-> notifyDataSetChanged()调用中对data-> notifyDataSetChanged()进行排序)
总结以上是内存溢出为你收集整理的android – 覆盖notifyDataSetChanged()全部内容,希望文章能够帮你解决android – 覆盖notifyDataSetChanged()所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)