@MainThread public class CursorDatasource extends Object implements Datasource<Cursor>, Closeable
Cursor as the underlying data storage.
Note that CursorDatasource does not allow to merge cursors,
but you are able to create that datasource with MergeCursor instance
when you want to merge a few cursors to the new one.
CursorDatasource implements Closeable interface to release
the underlaying cursor instance. Make sure you close this datasource when
you don't need its data anymore, for example, when Activity.onDestroy() or
Fragment#onDestroyView() are called.
This example illustrates the one of possible ways to release a datasource:
Cursor,
MergeCursor| Constructor and Description |
|---|
CursorDatasource(Cursor cursor) |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
Cursor |
get(int position)
Returns the element at the specified position.
|
void |
registerDatasourceObserver(DatasourceObserver observer)
Register a new observer to listen for data changes.
|
int |
size()
Returns the number of elements in this datastore.
|
void |
unregisterDatasourceObserver(DatasourceObserver observer)
Unregister an observer currently listening for data changes.
|
@NonNull public Cursor get(int position)
get in interface Datasource<Cursor>position - position of the element to return.public int size()
size in interface Datasource<Cursor>public void close()
close in interface Closeableclose in interface AutoCloseablepublic void registerDatasourceObserver(@NonNull DatasourceObserver observer)
The datasource may publish a variety of events describing specific changes.
Not all datasource may support all change types and some may fall back to a generic
DatasourceObserver.onChanged() "something changed" event
if more specific data is not available.
Components registering observers with a datasource are responsible for
unregistering those observers when finished.
registerDatasourceObserver in interface Datasource<Cursor>observer - Observer to register.Datasource.unregisterDatasourceObserver(DatasourceObserver)public void unregisterDatasourceObserver(@NonNull DatasourceObserver observer)
The unregistered observer will no longer receive events about changes to the datasource.
unregisterDatasourceObserver in interface Datasource<Cursor>observer - Observer to unregister.Datasource.registerDatasourceObserver(DatasourceObserver)