如何在Android ListView一侧显示字母

Pat*_*ham 34 android listview

我已经看了很多教程,让ListView在侧面有字母(如联系人列表),但他们似乎都使用了ListActivity类和/或数据库中的数据,而我只是使用了ListView(没有特殊的Activity)和一个ArrayList的数据.有谁知道如何在我自己的ListView的联系人列表中实现该字母滚动功能?

再次编辑

我按照这个教程,我认为最终会使它工作,但我仍然被迫关闭.

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);
        for (int i = 0; i < data.size(); i++)
        {
            String s = data.get(i).substring(0, 1).toUpperCase();
            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);

    }

    public int getPositionForSection(int section)
    {   
        return alphaIndexer.get(sections[section]);
    }

    public int getSectionForPosition(int position)
    {
        return 1;
    }

    public Object[] getSections()
    {
        return sections;
    }
}
Run Code Online (Sandbox Code Playgroud)

在LogCat中,它说java.lang.RuntimeException: Unable to resume activity {(package of another app I made) android.app.SuperNotCalledExcpetion.我觉得这很奇怪,因为我没有在我正在使用的那个应用程序中引用其他应用程序.我尝试卸载其他应用程序,仍然被迫关闭,但我可以看到LogCat说的是因为它没有更新.

最终编辑

这是工作代码.很抱歉发布了超过9个月的内容.

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)
    {
        for ( int i = sections.length - 1; i >= 0; i-- ) {
            if ( position >= alphaIndexer.get( sections[ i ] ) ) {
                return i;
            }
        }
        return 0;
    }

    public Object[] getSections()
    {
        return sections;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:如果您希望在滚动时滚动条显示在正确的位置,则getSectionForPosition非常重要.仅返回1(或0)表示您只是在第一个(或第二个)部分滚动,这将导致滚动条错误的位置(大部分超出屏幕).添加的代码返回相应的部分,以便滚动条可以知道它实际上在哪里.

Pat*_*ham 21

我忘了实例化alphaIndexer.现在工作得很好.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)