AlphabetIndexer with Custom Adapter

拈花ヽ惹草 提交于 2019-12-17 18:24:31

问题


Can someone show me an example of how to use AlphabetIndexer with a Custom Adapter that uses a getView? I have it working with a standard adapter, but have no clue how to implement it with a custom adapter.

Thanks


回答1:


If you're using a LoaderManager to manage your adapter's cursor, you'll want to make a small adjustment and override your adapters swapCursor method:

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

Everything else remains just as @vsm describes.




回答2:


Hi this is how I use 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 is the index of the column in database schema.

...

If this is not what you need, please add some code about which should be the class to extend and so on.

Anyway I hope this helps.



来源:https://stackoverflow.com/questions/4115920/alphabetindexer-with-custom-adapter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!