Im trying to implement a FragmentPagerAdapter
with a cursor loader. However, it seems that I am unable to access the cursor within getItem()
as I get a null reference error when attempting to access the cursor as it is not been set yet in onLoadFinished()
.
Will onLoadFinished()
not complete until getItem()
is finished?
Is there a better way to load the cursor data? I can query the ContentProvider
directly and store the results in my cursor, but thought a loader is preferred.
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
}
}
}