sergejsha / pinned-section-listview

Easy to use ListView with pinned sections for Android.
2.59k stars 808 forks source link

IndexOutOfBoundsException after NotifyDataSetChanged() #30

Closed bladeFury closed 10 years ago

bladeFury commented 10 years ago

After called NotifyDataSetChanged(), when recreating pinned shadow:

void recreatePinnedShadow() {
        destroyPinnedShadow();
        ListAdapter adapter = getAdapter();
        if (adapter != null && adapter.getCount() > 0) {
            int firstVisiblePosition = getFirstVisiblePosition(); // here the visible position is of the old dataset
            int sectionPosition = findCurrentSectionPosition(firstVisiblePosition);
            if (sectionPosition == -1) return; // no views to pin, exit
            ensureShadowForPosition(sectionPosition,
                    firstVisiblePosition, getLastVisiblePosition() - firstVisiblePosition);
        }
}

Looks like getFirstVisiblePosition () will return the visible position of the old dataset.

sergejsha commented 10 years ago

Could you please attach whole stack trace?

bladeFury commented 10 years ago

sure.

FATAL EXCEPTION: main
Process: com.hb.examples.pinnedsection, PID: 18532
java.lang.IndexOutOfBoundsException: Invalid index 29, size is 1
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
    at java.util.ArrayList.get(ArrayList.java:308)
    at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
    at com.hb.examples.PinnedSectionListActivity$SimpleAdapter.getItemViewType(PinnedSectionListActivity.java:118)
    at com.hb.views.PinnedSectionListView.findCurrentSectionPosition(PinnedSectionListView.java:305)
    at com.hb.views.PinnedSectionListView.recreatePinnedShadow(PinnedSectionListView.java:316)
    at com.hb.views.PinnedSectionListView$2.onChanged(PinnedSectionListView.java:135)
    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 com.hb.examples.PinnedSectionListActivity.onListItemClick(PinnedSectionListActivity.java:238)
    at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
    at android.widget.AbsListView$3.run(AbsListView.java:3638)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4998)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

PS: I was wondering is recreating pinned shadow necessary after data set change?

IncorporateApps commented 10 years ago

This is not fixed, it is still happening!

bladeFury commented 10 years ago

@IncorporateApps the pull request is not accepted yet, you could try this:https://github.com/bladeFury/pinned-section-listview

CCSnoopDog commented 10 years ago

how to 'called NotifyDataSetChanged()'

SimpleAdapter. NotifyDataSetChanged() ; nothing

danikula commented 10 years ago

Same issue #46 and fix for it

sergejsha commented 10 years ago

Must be fixed now.