Question

I am using SectionIndexer for an ArrayAdapter and have fast scrolling enabled. Most lists work fine, but a couple crash at the same point and show the same type of log.

(Please note: The ListView is based on data inputted from a MySQL table. The list has about 2000 items. But the SQL in the list has a "where clause" where only 100-200 may show at a time. It appears the thumb scroller naturally scrolls the size of the 2000 items, and is not corresponding to this "where clause". I don't believe this is a PHP/MySQL issue but in my Android/Java.)

Here is log:

08-27 07:26:33.360: E/AndroidRuntime(9145): FATAL EXCEPTION: main
08-27 07:26:33.360: E/AndroidRuntime(9145): java.lang.ArrayIndexOutOfBoundsException: length=21; index=21-
08-27 07:26:33.360: E/AndroidRuntime(9145):     at com.---.---.ItemAdapter.getPositionForSection(ItemAdapter.java:57)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.widget.FastScroller.getThumbPositionForListPosition(FastScroller.java:650)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.widget.FastScroller.onScroll(FastScroller.java:458)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1325)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5067)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4205)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.view.Choreographer.doFrame(Choreographer.java:524)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.os.Handler.handleCallback(Handler.java:615)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.os.Looper.loop(Looper.java:137)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at android.app.ActivityThread.main(ActivityThread.java:4928)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at java.lang.reflect.Method.invokeNative(Native Method)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at java.lang.reflect.Method.invoke(Method.java:511)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
08-27 07:26:33.360: E/AndroidRuntime(9145):     at dalvik.system.NativeStart.main(Native Method)

Here is code by Line 57:

public int getPositionForSection(int section) {
    return alphaIndexer.get(sections[section]);
}

Also, here is more code in the adapter:

alphaIndexer = new HashMap<String, Integer>();
int size = objects.length;

for (int i = 0; i < size; i++) {

    ItemObject it = objects[i];
    String name = it.name;
    String s = name.substring(0, 1);
    s = s.toUpperCase();

    if (!alphaIndexer.containsKey(s)) {
        alphaIndexer.put(s, i);
    }
}

Set<String> sectionLetters = alphaIndexer.keySet();
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];

// sectionList.toArray(sections);

for (int i = 0; i < sectionList.size(); i++)
    sections[i] = sectionList.get(i);


public int getSectionForPosition(int position)

{
    int closestIndex = 0;
    int latestDelta = Integer.MAX_VALUE;

    for(int i=0; i < sections.length; i++) {

        Log.d("Sections Length: ", String.valueOf(sections.length));

        int current = alphaIndexer.get(sections[i]);
        if(current == position) {
            //If position matches an index, return it immediately
            return i;
        } else if(current < position) {
            //Check if this is closer than the last index we inspected
            int delta = position - current;
            if(delta < latestDelta) {
                closestIndex = i;
                latestDelta = delta;
            }
        }
    }

    return closestIndex;
}

public Object[] getSections() {
    return sections;
}
Was it helpful?

Solution

Her is my final code that worked:

    int size = objects.length;


        for (int i = 0; i < size; i++) {

            ItemObject it = objects[i];
            String name = it.name;
            String s = name.substring(0, 1);
            s = s.toUpperCase();

            if (!alphaIndexer.containsKey(s)) {

                alphaIndexer.put(s, i);
            }
        }

        Set<String> sectionLetters = alphaIndexer.keySet();
        ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
        Collections.sort(sectionList);
        sections = new String[sectionList.size()];
        for (int i = 0; i < sectionList.size(); i++) {
            sections[i] = sectionList.get(i);
        }

    }


public int getPositionForSection(int section) {

        int maxSize = sections.length - 1;

        if (section > maxSize) {
            return 0;
        } else {

            return alphaIndexer.get(sections[section]);
        }
    }

    public int getSectionForPosition(int position)

    {
        int closestIndex = 0;
        int latestDelta = Integer.MAX_VALUE;

        for (int i = 0; i < sections.length; i++) {

            int current = alphaIndexer.get(sections[i]);
            if (current == position) {
                // If position matches an index, return it immediately
                return i;
            } else if (current < position) {
                // Check if this is closer than the last index we inspected
                int delta = position - current;
                if (delta < latestDelta) {
                    closestIndex = i;
                    latestDelta = delta;
                }
            }
        }

        return closestIndex;
    }

OTHER TIPS

Please see https://code.google.com/p/android/issues/detail?id=33293

They suggest to check the bounds of the array:

@Override
public int getPositionForSection(int section) {
    if (section > sections.length - 1) {
        return mItems.size() - 1;
    } else {
        return alphaIndexer.get(sections[section]);
    }
}

The documentation for both methods getPositionForSection and getSectionForPosition says:

If the section's starting position is outside of the adapter bounds, the position must be clipped to fall within the size of the adapter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top