title: How to show alphabetical letters on side of Android ListView
body: <p>I've looked at a lot of tutorials for making a ListView have the alphabetical letters on the side (like the Contacts list), but they all seem to using a ListActivity class and/or data from a database while I'm just using a ListView (no special Activity) and an ArrayList of data. Does anyone know how I can implement that alphabetical-scroll feature in the Contacts list for my own ListView? </p>
  
  <p><strong>Edited Again</strong></p>
  
  <p>I followed <a href="http://web.archive.org/web/20151120011714/http://twistbyte.com:80/tutorial/android-listview-with-fast-scroll-and-section-index" rel="nofollow noreferrer">this tutorial</a>, which I thought would finally make it work, but I'm still getting a forced close.</p>
  
  <pre><code>class AlphabeticalAdapter extends ArrayAdapter&lt;String&gt; implements SectionIndexer
  {
      private HashMap&lt;String, Integer&gt; alphaIndexer;
      private String[] sections;
  
      public AlphabeticalAdapter(Context c, int resource, List&lt;String&gt; data)
      {
          super(c, resource, data);
          for (int i = 0; i &lt; data.size(); i++)
          {
              String s = data.get(i).substring(0, 1).toUpperCase();
              alphaIndexer.put(s, i);
          }
  
          Set&lt;String&gt; sectionLetters = alphaIndexer.keySet();
          ArrayList&lt;String&gt; sectionList = new ArrayList&lt;String&gt;(sectionLetters);
          Collections.sort(sectionList);
          sections = new String[sectionList.size()];
          sectionList.toArray(sections);
  
      }
  
      public int getPositionForSection(int section)
      {   
          return alphaIndexer.get(sections[section]);
      }
  
      public int getSectionForPosition(int position)
      {
          return 1;
      }
  
      public Object[] getSections()
      {
          return sections;
      }
  }
  </code></pre>
  
  <p>In the LogCat, it says <code>java.lang.RuntimeException: Unable to resume activity {(package of another app I made) android.app.SuperNotCalledExcpetion</code>. I thought that was really weird because I don't reference that other app at all in the one I'm working with. I tried uninstalling that other app, still got the forced close, but I could see what the LogCat said because it didn't update.</p>
  
  <p><strong>Final Edit</strong></p>
  
  <p>Here is the working code. Sorry for posting it something like 9 months overdue.</p>
  
  <pre><code>class AlphabeticalAdapter extends ArrayAdapter&lt;String&gt; implements SectionIndexer
  {
      private HashMap&lt;String, Integer&gt; alphaIndexer;
      private String[] sections;
  
      public AlphabeticalAdapter(Context c, int resource, List&lt;String&gt; data)
      {
          super(c, resource, data);
          alphaIndexer = new HashMap&lt;String, Integer&gt;();
          for (int i = 0; i &lt; data.size(); i++)
          {
              String s = data.get(i).substring(0, 1).toUpperCase();
              if (!alphaIndexer.containsKey(s))
                  alphaIndexer.put(s, i);
          }
  
          Set&lt;String&gt; sectionLetters = alphaIndexer.keySet();
          ArrayList&lt;String&gt; sectionList = new ArrayList&lt;String&gt;(sectionLetters);
          Collections.sort(sectionList);
          sections = new String[sectionList.size()];
          for (int i = 0; i &lt; sectionList.size(); i++)
              sections[i] = sectionList.get(i);   
      }
  
      public int getPositionForSection(int section)
      {   
          return alphaIndexer.get(sections[section]);
      }
  
      public int getSectionForPosition(int position)
      {
          for ( int i = sections.length - 1; i &gt;= 0; i-- ) {
              if ( position &gt;= alphaIndexer.get( sections[ i ] ) ) {
                  return i;
              }
          }
          return 0;
      }
  
      public Object[] getSections()
      {
          return sections;
      }
  }
  </code></pre>
  
  <p>Edit : getSectionForPosition is important if you want your scroller appear in the right place while you are scrolling. Returning only 1 (or 0) tells that you are just scrolling in the first (or second) section which will result in scroller's wrong position (mostly goes out of screen). Added code returns the appropriate section so scroller can know where it actually is.</p>