带自定义适配器的AlphabetIndexer

时间:2010-11-07 00:27:09

标签: android adapter

有人可以向我展示如何将AlphabetIndexer与使用getView的自定义适配器一起使用的示例吗?我使用标准适配器,但不知道如何使用自定义适配器实现它。

由于

2 个答案:

答案 0 :(得分:8)

如果您使用LoaderManager来管理适配器的光标,则需要进行小幅调整并覆盖适配器swapCursor方法:

public Cursor swapCursor(Cursor c) {
    // Create our indexer
    if (c != null) {
        mIndexer = new AlphabetIndexer(c, c.getColumnIndex(Books.TITLE),
                " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
     }
     return super.swapCursor(c);
 }

其他所有内容都与@vsm描述的一样。

答案 1 :(得分:7)

这就是我使用AlphaIndexer的方式

private final class ContactListItemAdapter extends ResourceCursorAdapter
        implements SectionIndexer {
    AlphabetIndexer alphaIndexer;

    public ContactListItemAdapter(Context context, int layout, Cursor c) {
        super(context, layout, c);
        alphaIndexer = new AlphabetIndexer(c, NAME_COLUMN_INDEX,
                " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }   

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            .... 
            a normal getView
            ....
    }  

    public int getPositionForSection(int section) {
        return alphaIndexer.getPositionForSection(section);
    }

    public int getSectionForPosition(int position) {
        return alphaIndexer.getSectionForPosition(position);
    }

    public Object[] getSections() {
        return alphaIndexer.getSections();
    }
}

NAME_COLUMN_INDEX是数据库架构中列的索引。

...

如果这不是您所需要的,请添加一些代码,这些代码应该是要扩展的类等等。

无论如何,我希望这会有所帮助。

相关问题