The app has data in a SQLite database. The UI is primarily a RecyclerView. The question is how to best to transfer data from the database into the UI, whilst keeping off the main thread?
I originally planned to use a CursorLoader, ContentProvider, and RecyclerView. But reading around it looks like RecyclerView has no out-of-the-box support for Cursor-supplied data. Dang.
That then leaves me with a few other options...
AsyncTask to load the data, put it into model objects, and pass into the RecyclerView Adapter. Aside from being ugly, it isn't config-change friendly.
A custom Loader that loads the data from SQL and pushes it into model objects.
Use a Cursor loader, and when it returns the Cursor iterate through it to push the data into model objects. I suspect this would occur on the main thread and may damage performance.
Use Otto to send a request message to request data, and then return a model objects collection by return message. There may be ~500 objects so I think I may rather abusing Otto doing this.
If I am using a collection of model objects instead of a Cursor I see less benefit to a ContentProvider, and I also lose the ability for the UI to auto-refresh on data changes (which may be useful).
None of these options appeal much, is there a better way? The app is under time pressure so whatever it is needs to be fairly quick to implement. Unfortunately the UI needs to scroll horizontally and only targets Lollipop, so RecyclerView does seem a better bet than ListView.
xml. Create an another layout file (list_row. xml) in /res/layout folder to show the data in listview, for that right click on layout folder à add new Layout resource file à Give name as list_row.
We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.
use this simple adapter https://gist.github.com/Shywim/127f207e7248fe48400b, alternatively you could use android.support.v17.leanback.widget.ItemBridgeAdapter with android.support.v17.leanback.widget.CursorObjectAdapter but why to make own life harder?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With