Indexable ListView - Tutorial (source code download)

Updated: 25 April 2013

Tested in:
HTC desire S (android 2.3.5), Samsung Galaxy Ace (android 2.3.4), Samsung Galaxy Tab P3100 (android 4.0.4)

Base on:
Eclipse Indigo build in 20120216-1857, Java 1.6, Android 4.1.2

Download Source code:
DemoIndexableListView20130425.rar

Description:
Find an item quickly by its first character in ListView.

Screen shot:



Tutorial:
1. Copy 2 files in package ...widget
     IndexableListView.java
     IndexScroller.java

2. Modify your adapter to implement android.widget.SectionIndexer
@Override

    public int getPositionForSection(int section) {
        for (int j = 0; j < getCount(); j++) {
            if (section == 0) {
                // For numeric section
                for (int k = 0; k <= 9; k++) {
                    String text = null;
                    try {
                        text = mCurrentList.get(j);
                    } catch (Exception e) {
                    }
                    if (text == null)
                        return 0;
                    else if (String.valueOf(text.charAt(0)).toLowerCase().equals(String.valueOf(String.valueOf(k)).toString().toLowerCase()))
                        return j;
                }
            } else {
                String artist = null;
                try {
                    artist = mCurrentList.get(j);
                } catch (Exception e) {
                }
                if (artist == null)
                    return 0;
                else if (String.valueOf(artist.charAt(0)).toLowerCase().equals(String.valueOf(mSections.charAt(section)).toString().toLowerCase())) {
                    return j;
                }
            }
        }
        return 0;
    }

    @Override
    public int getSectionForPosition(int position) {
        return 0;
    }

    @Override
    public Object[] getSections() {
        String[] sections = new String[mSections.length()];
        for (int i = 0; i < mSections.length(); i++)
            sections[i] = String.valueOf(mSections.charAt(i));
        return sections;
    }


4 Responses to Indexable ListView - Tutorial (source code download)

  1. Anonymous says:
    This comment has been removed by the author.
  2. Anonymous says:
    This comment has been removed by the author.
  3. Nice Tutorial. But if i am using Checkbox on each listview item, then checkbox click is not working, if i am using Listview instead of Indexable listview , click events are working fine.

    Please help me.

  4. Can we use it in fragment ?

Leave a Reply

Powered by Blogger.