0

I have a method to display note title and date from a database. However method startManagingCursor is deprecated. Is there any way I can re-write this using cursor loader ?

private void fillData() {
    // Get all of the notes from the database and create the item list
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);


    String[] from = new String[] { NotesDbAdapter.KEY_TITLE ,NotesDbAdapter.KEY_DATE};
    int[] to = new int[] { R.id.text1 ,R.id.date_row};

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter notes =
        new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
    setListAdapter(notes);
}
Dodi
  • 2,201
  • 4
  • 30
  • 39
  • Have you taken a look at http://stackoverflow.com/questions/10145735/how-to-properly-transition-from-startmanagingcursor-to-cursorloader – aquaraga Sep 08 '13 at 10:52

1 Answers1

0

The regular CursorLoader is based on Android's ContentProvider - so if you're not using it, you'll have to write your own Loader. Here's one way you can do it:

https://github.com/commonsguy/cwac-loaderex/blob/master/src/com/commonsware/cwac/loaderex/acl/AbstractCursorLoader.java

From that point it's pretty straight forward. Your activity/fragment can be a callback for the LoaderManager - it will need to implement LoaderCallbacks interface.

Instead of running your query, use getLoaderManager().initLoader(this, null, LOADER_ID) to initialize data loading. Here's an example: http://developer.android.com/reference/android/app/LoaderManager.html

Now, the data will be in the onLoadFinished() callback. If you did everything right, you'll get your cursor here. Now simply use the cursor to populate your list.

Bartek Filipowicz
  • 1,235
  • 7
  • 9