package com.nietky.librarythingbrowser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.SectionIndexer;

class SectionIndexingArrayAdapter<T> extends ArrayAdapter<T> implements SectionIndexer {
    List<T> objects;
    
    HashMap<String, Integer> sectionsMap = new HashMap<String, Integer>();
    ArrayList<String> sectionsList = new ArrayList<String>();
    
    ArrayList<Integer> sectionForPosition = new ArrayList<Integer>();
    ArrayList<Integer> positionForSection = new ArrayList<Integer>();

    public SectionIndexingArrayAdapter(Context context, int textViewResourceId, List<T> objects) {
        super(context, textViewResourceId, objects);
        
        for (int i = 0; i < objects.size(); i++) {
            String objectString = objects.get(i).toString();
            if (objectString.length() > 0) {
                String firstLetter = objectString.substring(0, 1).toUpperCase();
                if (!sectionsMap.containsKey(firstLetter)) {
                    sectionsMap.put(firstLetter, sectionsMap.size());
                    sectionsList.add(firstLetter);
                }
            }
        }
        for (int i = 0; i < objects.size(); i++ ) {
            String objectString = objects.get(i).toString();
            if (objectString.length() > 0) {
                String firstLetter = objectString.substring(0, 1).toUpperCase();
                if (sectionsMap.containsKey(firstLetter)) {
                    sectionForPosition.add(sectionsMap.get(firstLetter));
                } else
                    sectionForPosition.add(0);
            } else 
                sectionForPosition.add(0);
        }
        for (int i = 0; i < sectionsMap.size(); i++)
            positionForSection.add(-1);
        for (int i = 0; i < sectionsMap.size(); i++) {
            for (int j = 0; j < objects.size(); j++) {
                Integer section = sectionForPosition.get(j);
                if (section == i) {
                    positionForSection.set(i, j);
                    break;
                }
            }
        }
        if (positionForSection.get(0) == -1)
            positionForSection.set(0, 0);
        for (int i = 1; i < sectionsMap.size(); i++)
            if (positionForSection.get(i) == -1)
                positionForSection.set(i, positionForSection.get(i - 1));
    }
    public int getPositionForSection(int section) {
        return positionForSection.get(section);
    }

    public int getSectionForPosition(int position) {
        return sectionForPosition.get(position);
    }

    public Object[] getSections() {
        return sectionsList.toArray();
    }
}
@@ -320,16 +320,24 @@
 plements
+%0A       
  Section
@@ -350,33 +350,8 @@
 r %7B%0A
-    List%3CT%3E objects;%0A    
 %0A   
@@ -484,20 +484,16 @@
 ing%3E();%0A
-    
 %0A    Arr
@@ -705,16 +705,28 @@
 ourceId,
+%0A           
  List%3CT%3E
@@ -790,24 +790,25 @@
 jects);%0A
+%0A
         
 %0A       
@@ -799,16 +799,375 @@
         
+// Note that List%3CT%3E objects has already been sorted alphabetically%0A        // e.g. with Collections.sort(objects) **before** being passed to%0A        // this constructor.%0A        %0A        // Figure out what the sections should be (one section per unique%0A        // initial letter, to accommodate letters that might be missing,%0A        // or characters like ,)
 %0A       
@@ -1613,32 +1613,105 @@
     %7D%0A        %7D%0A
+        %0A        // Calculate the section for each position in the list.%0A
         for (int
@@ -1741,17 +1741,16 @@
 e(); i++
- 
 ) %7B%0A    
@@ -2154,17 +2154,16 @@
   %7D else
- 
 %0A       
@@ -2200,32 +2200,108 @@
 d(0);%0A        %7D%0A
+        %0A        // Calculate the first position where each section begins.%0A
         for (int
@@ -2372,18 +2372,17 @@
 ion.add(
--1
+0
 );%0A     
@@ -2710,272 +2710,45 @@
     
+%7D%0A
     
-if (positionForSection.get(0) == -1)%0A            positionForSection.set(0, 0);%0A        for (int i = 1; i %3C sectionsMap.size(); i++)%0A            if (positionForSection.get(i) == -1)%0A                positionForSection.set(i, positionForSection.get(i - 1));%0A    %7D
+%0A    // The interface methods.%0A
 %0A   

Remove unnecessary code.