ListView SimpleCursorAdapter Update - android

I have a ListActivity with a SimpleCursorAdapter, thus the list view contains data from a database. If I am looking at the list I want it to update (re-query) itself when new data is put in the database being used by the SimpleCursorAdapter. Is the best way to do this to set a content observer on the database and call a requery() on the cursor when new content comes in ?

Is the best way to do this to set a content observer on the database and call a requery() on the cursor when new content comes in?
Yes, when there are changes in the database ContentObserver, you should refresh the cursor by calling requery(). Now this method is deprecated though, just request a new cursor, so you can do this asynchronously and update your list view once the new cursor arrived.

Related

Why there is no cursor adapter for RecyclerView?

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.

Listview not updating on inserting the item to database using cwac-loaderex

i'm using Sqliteloader by commnsware , this looks pretty simple because without help of content provider i'm able to use the loader functionality just by using simple SQL querys.
Now, on tapping on a button, i'm filling a form and insert the item to database like this.
ContentValues values = new ContentValues();
values.put(Tbldata.ACCOUNT, getActiveAccount());
values.put(Tbldata.time, dateobj.getTime());
getWritableDatabase().insert('Tbldata', 'ACCOUNT', values);
After this call, i will be calling finish() on my activity and it returns to my listview. Here the listview item is not updated
What is the best way to update listview?, should i call requery like this?
mLoaderManager.initLoader(0, null, this);
is it correct way of doing?,
What is the best way to update listview?
Call insert() on the SQLiteCursorLoader, not on the SQLiteDatabase. Quoting the documentation:
If you use the insert(), update(), delete(), replace(), and
execSQL() methods on SQLiteCursorLoader, the loader
framework will automatically update you to reflect a new
Cursor with the changed data. These methods take
the same parameters as they do on SQLiteDatabase.
Note that I am no longer maintaining SQLiteCursorLoader.

how to know value is added in simplecursoradapter

I need to know, there is any alert function for SimpleCursorAdapter when value is added? What I want is
I open MainActivity, there is ListView integrated with
SimpleCursorAdapter added data by background services.
When value is added by background services, I want to refresh ListView and want to
know how many new value is added. (don't want to use notification).
Just like AddAll() and notifyDataSetChanged() of ArrayAdapter.
It can be possible?
The best way to do this to set a content observer on the database and use CursorLoader to load data asynchronously.
Request a new cursor on addition of new records
cited:
requery()
This method was deprecated in API level 11. Don't use this. Just request a new cursor, so you can do this asynchronously and update your list view once the new cursor comes back.

Android Cursor Management w/ListView

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.

How to refresh a ListView to requery its cursor to repopulate its data and its views

I have a ListView which uses a CursorAdatper as its adapter.
I would like to have the list view to
requery its data
refresh its view once the requery is done.
I tried:
CursorAdapter adapter = (CursorAdapter)listView.getAdapter();
adapter.notifyDataSetChanged();
And i tried:
CursorAdapter adapter = (CursorAdapter)listView.getAdapter();
adapter.getCursor().requery();
but none worked. I have set a break point in my ContentProvider's query method, but I don't see the requery being called or my ListView getting refreshed with new data.
Can you please tell me what is the solution to my problem?
Thank you.
Calling requery() on a database or content provider Cursor attached to ListView via a CursorAdapter automatically refreshes the list (your second scenario above). You can see some examples of this. If that is not working for you, there may be some bug in your ContentProvider or adapter.
In the new API (using loader manager) you can use:
getLoaderManager().getLoader(_YOUR_LOADER_ID_).forceLoad();
you can try: adapter.changeCursor(new cursor). It work for me
I know this answer is probably too late but fyi to others, if you are using a cursor loader (as you probably should do instead), just do
getContext().getContentResolver().notifyChange(uri, null);
where uri is the same uri used for the cursor loader.

Categories

Resources