How to show alphabetical letters on side of Android ListView

前端 未结 2 835
有刺的猬
有刺的猬 2020-11-30 22:45

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

相关标签:
2条回答
  • 2020-11-30 22:55

    If I understand you correctly, you want to set fastScrollEnabled=true and that will give you the little thumb scroller on the right. If you want to add a floating letter like the one in contacts, there is a great example in the APIDemos project in List9.java (http://www.devdaily.com/java/jwarehouse/android-examples/platforms/android-2/samples/ApiDemos/src/com/example/android/apis/view/List9.java.shtml)

    0 讨论(0)
  • 2020-11-30 23:22

    I forgot to instantiate alphaIndexer. Works perfectly now.

    class AlphabeticalAdapter extends ArrayAdapter<String> implements SectionIndexer
    {
        private HashMap<String, Integer> alphaIndexer;
        private String[] sections;
    
        public AlphabeticalAdapter(Context c, int resource, List<String> data)
        {
            super(c, resource, data);
            alphaIndexer = new HashMap<String, Integer>();
            for (int i = 0; i < data.size(); i++)
            {
                String s = data.get(i).substring(0, 1).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)
        {   
            return alphaIndexer.get(sections[section]);
        }
    
        public int getSectionForPosition(int position)
        {
            return 1;
        }
    
        public Object[] getSections()
        {
            return sections;
        }
    }
    
    0 讨论(0)
提交回复
热议问题