How to automatically re-query using LoaderManager

I have an application that shows data from SQLite DB, and the data is constantly changing, so obviously I thought I should use LoaderManager to display the data.

I read a little about using LoaderManager with SQLite and saw Alex Lokwood 's answer about implementing CursorLoader and using it, and I saw this answer about using CursorLoader directly in SQLite DB instead of ContentProvider and started using commonsware Loaderex .

I created this class to display data in a ListView:

public class LandingPageActivity extends FragmentActivity implements LoaderCallbacks<Cursor> { ListView list; SimpleCursorAdapter mAdapter; private SQLiteCursorLoader loader = null; private Context mContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_friends_and_threads); list = (ListView) findViewById(R.id.list); mContext = LandingPageActivity.this; mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, new String[] { Properties.Title.columnName }, new int[] { android.R.id.text1 }, 0); list.setAdapter(mAdapter); getSupportLoaderManager().initLoader(0, null, this); } @Override public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { Context c = mContext; String query = "SELECT * FROM TABLE_NAME"; SQLiteOpenHelper db = DatabaseHelper.getDatabase(); loader = new SQLiteCursorLoader(c, db, query, null); return loader; } @Override public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) { mAdapter.changeCursor(arg1); } @Override public void onLoaderReset(Loader<Cursor> arg0) { mAdapter.changeCursor(null); } } 

The problem starts when the database changes from the "BroadcastListener" while the activity is not already running.

Basically, what I'm looking for is real-time monitoring of database changes, but it seems (also from the Android documentation that I need to call the restartLoader function when I want to re-query db).

Is there a way to automate the re-request backstage?

UPDATE:

My initial thought was to create a function that restarts the bootloader, something like this:

 public static void restartLoader(Context context){ context.getSupportLoaderManager().restartLoader(0, null, LandingPageActivity.this); } 

but it seems a rude and not the best way to do it.

0
android commonsware-cwac android-sqlite android-loadermanager
Nov 20
source share
1 answer

the problem starts when the database changes from "BroadcastListener" while the activity is still running ... is there a way to automate the re-query backstage?

Use the BroadcastReceiver methods insert() , update() , replace() and delete() on SQLiteCursorLoader to modify the database. Then Cursor will be automatically loaded.

0
Nov 20 '12 at 12:31
source share



All Articles