SectionIndexer数组的Android错误日志(使用快速滚动)

时间:2012-08-27 14:32:08

标签: android android-arrayadapter fastscroll

我正在SectionIndexer使用ArrayAdapter并启用快速滚动功能。大多数列表工作正常,但在同一点上几个崩溃并显示相同类型的日志。

(请注意:ListView基于从MySQL表输入的数据。该列表包含大约2000个项目。但是列表中的SQL有一个“where子句”,其中只有100-200可以显示一个时间。看来拇指滚动器自然地滚动2000个项目的大小,并不对应于这个“where子句”。我不相信这是PHP / MySQL问题,但在我的Android / Java中。)

这是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)

以下是第57行的代码:

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

此外,适配器中还有更多代码:

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;
}

2 个答案:

答案 0 :(得分:1)

她是我的最终代码:

    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;
    }

答案 1 :(得分:0)

请参阅https://code.google.com/p/android/issues/detail?id=33293

他们建议检查数组的界限:

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

方法getPositionForSectiongetSectionForPosition的文档说明了:

  

如果部分的起始位置在适配器边界之外,   必须剪裁位置以使其落在适配器的大小范围内。