我试图FragmentPagerAdapter
用游标加载器实现一个。但是,似乎我无法访问其中的游标,getItem()
因为在尝试访问游标时出现空引用错误,因为它尚未在onLoadFinished()
.
onLoadFinished()
完成之前不会完成getItem()
?
有没有更好的方法来加载游标数据?我可以ContentProvider
直接查询并将结果存储在我的光标中,但认为加载器是首选。
public class MyAdapter extends FragmentPagerAdapter implements LoaderManager.LoaderCallbacks<Cursor>{
private Cursor cursor;
public MyAdapter(FragmentManager fm) {
super(fm);
getLoaderManager().initLoader(0, null, this);
}
@Override
public int getCount() {
return 13;
}
@Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
cursor.moveToPosition(position);
args.putString(TextViewFragment.TITLE, cursor.getString(1));
args.putInt(TextViewFragment.POSITION_KEY, position);
return TextViewFragment.newInstance(args);
}
@Override
public CharSequence getPageTitle(int position) {
return "Fragment # " + position;
}
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
// TODO Auto-generated method stub
CursorLoader cursorLoader = new CursorLoader(getActivity(), TestContentProvider.CONTENT_URI, null, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
// TODO Auto-generated method stub
cursor = arg1;
//throw new IllegalArgumentException("We are HEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHEREHERE");
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
}
}