由LoaderManager管理的具有自定义适配器的AlphabetIndexer

我正在尝试AlphabetIndexer使用自定义适配器来实现


带自定义适配器的AlphabetIndexer


我的类ContactsCursorAdapter可以扩展SimpleCursorAdapter和实现,SectionIndexer 并且我正在使用A LoaderManager来管理适配器的游标,因此我已经覆盖了swapCursor()上述示例的第二个答案所示的方法。


public class ContactsCursorAdapter extends SimpleCursorAdapter 

    implements SectionIndexer{


    private LayoutInflater mInflater;

    private Context mContext; 


    private AlphabetIndexer mAlphaIndexer;


    public ContactsCursorAdapter(Context context, int layout, Cursor c,

        String[] from, int[] to) {

        super(context, layout, c, from, to);


        mInflater = LayoutInflater.from(context);

        mContext = context;

    }


    public View getView(final int position, View convertView, ViewGroup parent) {

        ...

    }


    @Override

    public int getPositionForSection(int section) {

        return mAlphaIndexer.getPositionForSection(section);

    }


    @Override

    public int getSectionForPosition(int position) {

        return mAlphaIndexer.getSectionForPosition(position);

    }


    @Override

    public Object[] getSections() {

        return mAlphaIndexer.getSections();

    }


    public Cursor swapCursor(Cursor c) {

        // Create our indexer

        if (c != null) {

            mAlphaIndexer = new AlphabetIndexer(c, c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME),

                " ABCDEFGHIJKLMNOPQRSTUVWXYZ");

        }

        return super.swapCursor(c);

    }

}

但是,如果我将listview设置为fastScrollEnabled = true,则会出现此错误


getListView()。setFastScrollEnabled(true);


在我的类ContactsCursorLoaderListFragment中,该类扩展ListFragment并实现了LoaderManager.LoaderCallbacks。

setFastScrollEnabled()调用该方法后,它将调用自定义适配器的getSections()崩溃方法。


如果我对setFastScrollEnabled()呼叫进行评论,那么它不会出错,但是我看不到AlphabetIndexer工作原理。


因为我的自定义适配器设置为ListFragment而不是,这是否需要以不同的方式实现ListActivity?


有人对如何使这一切起作用有建议吗?


人到中年有点甜
浏览 336回答 1
1回答

MMMHUHU

所以我终于把这个工作了。这是我的做法:我补充说:ListView lv = getListView();lv.setFastScrollEnabled(true);lv.setScrollingCacheEnabled(true);onLoadFinished()交换新游标后的方法public void onLoadFinished(Loader<Cursor> loader, Cursor data) {&nbsp;&nbsp;// Swap the new cursor in.&nbsp; (The framework will take care of closing the&nbsp;// old cursor once we return.)&nbsp;mAdapter.swapCursor(data);&nbsp;ListView lv = getListView();&nbsp;lv.setFastScrollEnabled(true);&nbsp;lv.setScrollingCacheEnabled(true);}因此,这三个语句已从onActivityCreated()我的自定义方法中删除ListFragment。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android