我正在尝试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?
有人对如何使这一切起作用有建议吗?
MMMHUHU
相关分类