19

I updated my android SDK to the latest version, and now it says that startManagingCursor() is deprecated. I need help to update my code to use the new CursorLoader.

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);
    NoteAdapter notes = new NoteAdapter(this,  R.layout.notes_row, notesCursor);
    setListAdapter(notes);
}

So, startManagingCursor() is old, what would the new code look like, if it was translated?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
DreamHawk
  • 785
  • 2
  • 9
  • 20
  • 1
    CursorLoader works in adition to ContentProvider ... So it is not possible to "translate" rhis code .... – Selvin Apr 13 '12 at 18:01
  • http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html – user1422551 Aug 17 '12 at 14:14
  • Good to keep in mind this answer to the depreciated method. Turns out it runs on the main thread, which slows things down, that's why it was depreciated. [link](http://stackoverflow.com/questions/19651680/cursorloader-with-startmanagingcursor) – Azurespot Dec 04 '14 at 21:30

1 Answers1

16

First, startManagingCursor() still works. It is not ideal, in that it performs database I/O on the main application thread. In Android, "deprecated" generally means "we have something else that we think is better that we suggest you use". So, when it makes sense in the development cycle of your app, you should consider migrating.

Second, as Selvin noted, the SDK only provides a Loader implementation for a ContentProvider. I have a project that offers a Loader for SQLite directly.

Third, there really is no straight-up "translation" for your code. The Loader framework is asynchronous and event-driven, whereas your code is not.

Generally speaking, your Loader would be responsible for fetching the notes, and you would populate your ListView in onLoadFinished(), when you are delivered the Cursor from the Loader.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Just to add a thought here, if you are building NEW code you should never use deprecated API's. – Ethan Allen Oct 22 '12 at 19:54
  • 23
    @EthanAllen: Nonsense. For starters, it's impossible if you want to support older devices. For example, you cannot create a `PreferenceActivity` that works on API Level 10 and below without using deprecated methods. Deprecation warnings need to be considered on a case-by-case basis. *Generally* you want to avoid deprecated methods where possible. But "never" is overstating the case. – CommonsWare Oct 22 '12 at 20:11