I found a working cursorAdapter for RecyclerView gist. It works similarly as for listView. But I can't understand why there is no default cursor adapter. It is bad practice using cursor adapter and need manually get data from db convert to list of objects and then use it? Or what explanation for this?
But I can't understand why there is no default cursor adapter
Google elected not to create any concrete adapters for specific types of data collections.
You are certainly welcome to use a Cursor as the model data for a RecyclerView.Adapter. Just bear in mind that a Cursor treats the position as internal state. Make sure that your RecyclerView.ViewHolder pulls the data out of the Cursor and uses it, rather than holding onto the Cursor itself and assuming that it will always automatically be pointing to the correct row.
This sample app demonstrates a RecyclerView backed by a Cursor, in this case a Cursor obtained from querying the MediaStore ContentProvider.
Related
I've been attempting to implement drag and drop features into my notes app using ItemTouchHelper however I am struggling to figure out how to get it to work with cursors. I am currently retrieving stored note data from the database using a cursor loader and a content provider, and the adapter receives it's data from the returned cursor as shown.
public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder holder, int position) {
mCursor.moveToPosition(position);
String title = mCursor.getString(mCursor.getColumnIndex(NotesContract.COL_TITLE));
String body = mCursor.getString(mCursor.getColumnIndex(NotesContract.COL_BODY));
holder.titleText.setText(title);
holder.bodyText.setText(body);
}
The problem I am having is that I don't know how I would keep track of the position of the moved items in the list when I drag and drop them. I can't change the order of the rows in the cursor. I have only seen examples where the data source for the adapter is a List. I have considered copying my cursor data into a list of Note objects, so that I could rearrange them to reflect the changes made by dragging and dropping, but this seems to defeat all you gain by using cursors and cursor loader. Also when cursor loader finishes loading it will return a new cursor with all the results in the original order again and populate the adapter accordingly, so any changes made to the order if my new List would be lost.
Does anyone know how to do this or am I going about this entirely the wrong way using cursors and a loader? Any help would be much appreciated. Thanks!
I have done this by adding a column named sort_order in the table to keep track of the sort order. When you drag and drop you update the sort_order in the onMove method in ItemTouchHelper.SimpleCallback.
A good tutorial how to implement drag and drop and swipe to dismiss you can find here
What exactly do you need that cursor for? If you have any specific question just add a comment. I worked a lot with this features!
I want to populate RecyclerView using database. As currently there is no inbuilt adapter for populating RecyclerView using database, I have used CursorRecyclerAdapter by Shywim. I have created a sample App to test it and it worked fine. The feature I didn't liked is having an _id column in the resultset and calling swapCursor() on each database operation, mostly insert and delete. This goes same with ListView when using SimpleCursorAdapter. My query is what if I use ArrayList as the dataset instead of directly using the Cursor.
Benefits of doing this(my assumption) :
No more a need of _id column in the resultset.
Can fetch the data from database, put it into ArrayList and close the cursor.
No need of calling swapCursor() on each database operation as I can add/remove specify elements from the ArrayList and call notifyDataSetChanged()
I don't know the exact logic behind swapCursor() and notifyDataSetChanged(). So, can't decide which one is light-weight and efficient.
If someone has experienced this or done this before, please clear my doubts. Any corrections and suggestions are most welcome.
Using array list and custom adapter is better way for this as per my understanding.
See some scenarios below :
1) Cursor will close after each transaction so database will work smoothly.
2) As you can close cursor on operation done so it will never generate cursor not closed exception.
3) You can modify view of each row easily and manage custom adapter as per your choice.
There are many other reasons, but in short custom adapter is better then cursor adapter as per my understanding.
I wrote my own CursorAdapter for RecyclerView like following link: https://gist.github.com/skyfishjy/443b7448f59be978bc59
Then I found whenever I change something in database and want to show it in RecyclerView, I need to create a new Cursor by db.query() and use CursorAdpater's changeCursor(). Since query() will scan all rows in database, the RecyclerView will refresh slowly when data amount is big even I insert only one row into database.
Besides, as we all know, RecyclerView provides notifyItemInserted/Removed(position) for developers so that the RecyclerView can refresh partly, which is useful and beneficial to memory/time. However, when I use CursorAdapter, I don't know when and how I can use these methods because changing cursor isn't adding something directly to dataset binding with RecyclerView but refreshing all items in fact.
So are there any better ways to show data from database in RecyclerView and use RecyclerView's improving method to show variety of database?
I can tell you what i've done...
A. Loaded a cursor using Loader.
B. Copied the cursor into arraylist that is attached to the adapter (the cursor isnt attached to the adapter directly), close the cursor. Works well if there isnt a lot of data - if there is a lot rows then i would have load some of it to the arraylist and then when the user would scroll down i would query again and load from the last row of the array.
C. When the user would like to delete or add something i would do the operation on arrayList first (UI thread) notifiyItemChanged and then change the db (Back thread)
Hope i helped.
I am new to Android and am trying to get my header round the SimpleCursorAdapter and CursorLoader classes. From my understanding, all of the examples that I have seen use these two classes to load data into a ListView in a background thread (to not block the UI).
I am OK with this and have used this general approach with my own list of items, however I now want to be able to click on an item in the list and get the full information for the item. Is it usual practice to use SimpleCursorAdapter and CursorLoader to retrieve the details for a single item? or are they just meant for lists?.
Thanks.
They are not meant for lists only. You can - and should - use them in detail views (activities) as well.
I've sketched a possible way to do so on my blog:
http://www.grokkingandroid.com/using-loaders-in-android/
Think of Adapters as a layer of abstraction between your data (Cursor) and whatever you attach that Adapter to (ListView for example). This way, you have a common interface between your data (Cursor, ArrayList, whatever) and the View you display that data on (ListView, TableView, etc.), this is helpful because if you later find that you want to access your data through an ArrayList rather than a Cursor, then you simply swap out the adapter with a different one and you're ready.
Now considering your question, Adapters give an abstract access to information, therefore you can "ask" it for what information is stored and where. You could attach an OnItemClickListener to your ListView and then access your data from there.
I've got a ListActivity that displays a list of items from a database using a CursorAdapter, which initially contains all items in the table. I also provide an EditText view where the user can enter search text, and as characters are entered, I requery the database using a "LIKE" or "MATCH" where clause to filter the results (IOW, what lots of apps do when searching).
Currently, I do this in an AsyncTask by creating a new Cursor from the query, creating a new instance of my CursorAdapter class, and then calling list.setAdapter from the UI thread when the task completes. This is all working, but is there a more elegant way of effectively requerying the database with a new WHERE clause from withing the existing adapter/cursor and avoiding having to create new object instances each time? Any examples of this technique?
Doug Gordon
GHCS Software
First of all, CursorAdapter has an changeCursor-Method, were you can change the Cursor without changing the adapter itself. When changing the Cursor the corresponding AdapterView will automatically be notified and update itself.
For further abstraction, you might provide a business object that wraps the query and optionally the execution of the query and provide it to both the Adapter and the TextView (or the Activity possessing the TextView). The TextView changes the business object, the object creates a new cursor and tells the Adapter (using the Observer Pattern, e.g. a Listener) that the cursor changed, and the Adapter retrieves the new Cursor and updates itself.