Override notifyDataSetChanged ()
I have a custom ArrayAdapter. When data is displayed in ListViews, I always want it to be sorted, so I tried to override notifyDataSetChanged () to sort the adapter first:
@Override
public void notifyDataSetChanged() {
sort(new Comparator<IceContact>() {
@Override
public int compare(IceContact iceContact, IceContact iceContact2) {
return iceContact.compareTo(iceContact2);
}
});
super.notifyDataSetChanged();
}
IceContact class is a get-set class that implements Comparable. Unfortunately, this gives me, ironically, a StackOverflowError (logcat below). Then I tried to do it the other way around. Override sort()
and call notifyDataSetChanged()
directly after that, for example:
@Override
public void sort(Comparator<? super IceContact> comparator) {
super.sort(comparator);
notifyDataSetChanged();
}
It works great! Can anyone explain why? It looks like they are nearly identical implementations.
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
source to share
IceContact class is a get-set class that implements Comparable. Unfortunately, this gives me, ironically, a StackOverflowError (logcat below).
sort()
calls by default (in its implementation) notifyDataSetChanged()
, which then calls again sort()
, which in turn will call notifyDataSetChanged()
, which will call sort()
, which will call ...
In sorting notifyDataSetChanged
data without also calling notifyDataSetChanged()
as sort()
will automatically do this for you.
The second scenario will work as you are basically calling notifyDataSetChanged()
twice ( super.sort()
, sort the data - > notifyDataSetChanged()
from super
call-> notifyDataSetChanged()
) from you)
source to share
A simple way to prevent the exception would be to disable the notifyDataSetChanged () call.
@Override
public void notifyDataSetChanged()
{
setNotifyOnChange(false);
sort(yourComparatorInstance);
super.notifyDataSetChanged();
}
It will be automatically activated according to the docs:
... and calling notifyDataSetChanged () resets the flag to true.
What is the type of sort ().
source to share