SimpleCursorAdapter alternative
Asked Answered
M

2

12

I'm using the deprecated SimpleCursorAdapter to display data from Cursor to ListView. I've added the additional argument 0, which removes the dreprecated warning, but I want to use a better way to display data. I've read something about Loader, but don't know how to implement it. What would be a better alternative to the code below? How would this code be translated to use Loader?

Cursor c = mDbHelper.getAllRecords();
    startManagingCursor(c); //this is also deprecated

    String[] from = new String[] { "Name" };
    int[] to = new int[] { R.id.text1 };

    SimpleCursorAdapter names =
        new SimpleCursorAdapter(this, R.layout.names_row, c, from, to, 0);
    setListAdapter(names);
Mycology answered 11/2, 2013 at 16:45 Comment(2)
"but I want to use a better way to display data." A Loader only reads the data from your database on another thread, it doesn't change the way the data is displayed. You should change names_row.xml to alter the way it is displayed.Adrianadriana
Switching to Loaders is not a trivial matter using only Android classes. You need to create a ContentProvider first and then implement the Loader framework. However CommonsWare has written a Loader library that doesn't require ContentProviders. Here is a tutorial that covers both approaches.Adrianadriana
P
7

adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, from, to, 1);

This will do the automatic requery. 1 is set to true and 0 to false.

Petronella answered 9/7, 2013 at 10:7 Comment(0)
B
6

SimpleCursorAdapter isn't deprecated, just the constructor.

see SimpleCursorAdapter deprecated in API version 15?

Biller answered 11/2, 2013 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.