Last active
November 17, 2017 15:18
-
-
Save emil10001/6445651 to your computer and use it in GitHub Desktop.
Basic Android Loader implementation, accompanying blog post: http://www.recursiverobot.com/post/60331340133/very-simple-example-of-a-loader-and-loadermanager-in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DumbLoader extends CursorLoader { | |
private static final String TAG = "DumbLoader"; | |
public DumbLoader(Context context) { | |
super(context); | |
} | |
@Override | |
public Cursor loadInBackground() { | |
// this is just a simple query, could be anything that gets a cursor | |
return DummyData.getItems(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> { | |
CursorAdapter mAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
// omitted for brevity ... | |
getSupportLoaderManager().initLoader(LOADER_ID, null, this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { | |
return new DumbLoader(this); | |
} | |
@Override | |
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { | |
// mAdapter is a CursorAdapter | |
mAdapter.swapCursor(cursor); | |
} | |
@Override | |
public void onLoaderReset(Loader<Cursor> cursorLoader) { | |
mAdapter.swapCursor(null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment