RecyclerView CursorAdapter Library

Today I released my first library which is for a RecyclerViewCursorAdapter.

Using a ListView to display database data becomes a lot easier when you use a CursorAdapter combined with a CursorLoader to display data from your ContentProvider. The main benefit of CursorLoader is explained in the docs:

This class implements the Loader protocol in a standard way for querying cursors, building on AsyncTaskLoader to perform the cursor query on a background thread so that it does not block the application’s UI.

The CursorAdapter helped by efficiently clearing the reference to the Cursor that it held, so that the responsibility didn’t lay on the developer.

When ListView was replaced by RecyclerView, it came with a different type of Adapter with no CursorAdapter implementation. After being inspired by a StackOverflow answer to see how a CursorAdapter could be used as the underlying data source of a RecyclerView.Adapter class. Once I had it working, I quickly abstracted the idea into a library, which you can find complete with samples on Github. The README there will explain how to include this library and how to get started using CursorAdapter again with your RecyclerView!

Share this post...
Share on Reddit0Share on Google+1Tweet about this on TwitterShare on LinkedIn0Share on Facebook0

3 Comments


  1. Hi Adam,
    Thanks for the library! Exactly what i was looking for, recyclerview together with an async cursorloader. (I used a Listfragment with its ListView before.)
    What i try to do now is to make each item in the list selectable by clicking on its radiobutton, the problem is that these buttons should be dynamically incapsulated in a radiogroup, as to select only one of them.
    How can this be done?


    1. I’m glad it’s proven useful for you! In short, what you will need to do is attach a click listener to the RadioButton on each item. Then, write a method that takes the adapter position and checks the appropriate item while unchecking the others. You’ll keep a variable at the adapter level such as mSelectedPosition to know which one is appropriate.

      How you do the checking may depend specifically on your project, the following gist assumes each item has an isChecked property and that it is used inside onBindView() to check the button, but perhaps you store it a different way. Regardless, I hope that this gist puts you on the right track, and also be aware to store that value if necessary in case of an orientation change.

      https://gist.github.com/androidessence/67c47f774b972fdcf4c2


      1. Hi Adam, Thanks a lot for your support and i agree that your suggestions point to the solution of this radio buttons problem !

Leave a Reply