带自定义适配器的AlphabetIndexer

mia*_*lle 9 android adapter

有人可以告诉我如何使用AlphabetIndexer自定义适配器使用getView?我使用标准适配器,但不知道如何使用自定义适配器实现它.

谢谢

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

其他所有内容都像@vsm描述的那样.


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

NAME_COLUMN_INDEX是数据库模式中列的索引.

...

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

无论如何,我希望这有帮助.

  • 你好,我想问:如果我没有从数据库中获取我的项目,我该怎么办? (2认同)