Getting SQLiteCursorLoader to monitor data changes

I am trying to implement a DataListFragment with an adapter that uses the Loader from Commonsware. This bootloader uses SQLiteDatabase directly and does not require the use of ContentProviders.

Android downloader links: "While bootloaders are active, they need to control the source of their data and provide new results when content changes."

In my implementation of SQLiteCursor (below) this does not happen. OnLoadFinished()called once and that he. Presumably, one could insert calls Loader.onContentChanged()where the base database will be modified, but overall the database code class is not aware of loaders, so I'm not sure of the best way to implement this.

Does anyone have any tips on how to “know the bootloader data”, or should I wrap the database stuff as a ContentProvider and use CursorLoader instead?

import com.commonsware.cwac.loaderex.SQLiteCursorLoader;

public class DataListFragment extends ListFragment implements    LoaderManager.LoaderCallbacks<Cursor>{

protected DataListAdapter  mAdapter;     // This is the Adapter being used to display the list data.
public SQLiteDatabase      mSqlDb;
private static final int   LOADER_ID = 1;

@Override 
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    int rowlayoutID = getArguments().getInt("rowLayoutID");
    // Create an empty adapter we will use to display the loaded data.
    // We pass 0 to flags, since the Loader will watch for data changes
    mAdapter = new DataListAdapter(getActivity(),rowlayoutID, null , 0);
    setListAdapter(mAdapter);
    // Prepare the loader.  Either re-connect with an existing one,
    // or start a new one.
    LoaderManager lm = getLoaderManager();
    // OnLoadFinished gets called after this, but never again.
    lm.initLoader(LOADER_ID, null, this);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String sql="SELECT * FROM "+TABLE_NAME+";";
    String[] params = null;
    SQLiteCursorLoader CursorLoader = new SQLiteCursorLoader(getActivity(), mSqlDb, sql, params);
    return CursorLoader;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the old cursor once we return.)
    mAdapter.swapCursor(data);
    // The list should now be shown.
    if (isResumed()) { setListShown(true);} 
    else { setListShownNoAnimation(true); }
}
public void onLoaderReset(Loader<Cursor> loader) {
    // This is called when the last Cursor provided to onLoadFinished()
    // above is about to be closed.  We need to make sure we are no
    // longer using it.
    mAdapter.swapCursor(null);
}
+4
source share
1 answer

The documentation Loaderhas flaws.

100% of the implementations Loaderbuilt into Android "control the data source and bring new results when the content changes." Since there is only one implementation Loaderbuilt into Android today , their documentation is as accurate as possible.

However, citing an update to my book, which should be released in an hour or two:

, . , , - , - , .

SQLiteCursorLoader, , . , Loader ( ).

, CursorLoader, , ContentProvider - , .

- , , SQLiteCursorLoader , , (, a Service), SQLiteCursorLoader .

+20

All Articles