を使用SectionIndexer
してArrayAdapter
おり、高速スクロールが有効になっています。ほとんどのリストは問題なく動作しますが、いくつかが同じ時点でクラッシュし、同じ種類のログが表示されます。
(注: これListView
は、MySQL テーブルから入力されたデータに基づいています。リストには約 2000 項目があります。しかし、リスト内の SQL には、一度に 100 ~ 200 個しか表示されない「where 句」があります。サム スクローラーに表示されます。自然に 2000 個のアイテムのサイズをスクロールし、この「where 句」に対応していません。これは PHP/MySQL の問題ではなく、私の Android/Java の問題だと思います。)
ログは次のとおりです。
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;
}