How to update ListView in case of CursorAdapter usage? - android

The reason I'm asking that is because requery() is deprecated. What is the best way now to refresh your ListView?

requery() updates a Cursor, not a CursorAdapter. As you say, it has been deprecated, and its replacement is:
oldCursor = myCursorAdapter.swapCursor(newCursor); // hands you back oldCursor
or:
myCursorAdapter.changeCursor(newCursor); // automatically closes old Cursor
myCursorAdapter.notifyDataSetChanged() notifies the ListView that the data set has changed, and it should refresh itself

Use BaseAdapter.notifyDataSetChanged().

You can create a new cursor and call changeCursor() (documentation here) on your CursorAdapter instance or call notifyDataSetChanged() (documentation here) on your adapter.

This is what works for me, im not sure its the best way.
c = db.rawQuery( "SELECT * FROM mytable", null); //same line of the first initialization
adapter.swapCursor(c);
I refresh the only cursor, I dont know what to do with a new one.
Also i dont know pepole that answer with only a name of a function.

The only thing that helped me, was to initialise new cursor, similar as previous one like:
cursor = dbHelper.myDataBase.rawQuery(StaticValues.SQL_CAT, null);
newCursor = dbHelper.myDataBase.rawQuery(StaticValues.SQL_CAT, null);
and then call:
adapter.changeCursor(newCursor);
that updated my listview.

I tried to add my response as a comment but failed for some reason.
cursoradapter.notifyDatasetchanged() should not work as your adapter is linked to a "cursor" which holds a "query" that was executed before dataset was changed. Hence one would require to change "cursor" by doing the "new query" and link to cursoradapter using cursoradapter changecursor().

Related

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.

Changing Cursor cause an Exception

I have a ListView in a dialog and an EditText to filter my list for CustomerCodes,i've implemented my filter query with a TextWatcher and in onTextChanged() i've changed my old Cursor with
Cursor FilteredCPCodeList = CustomerDBAdapter.instance.CursorFilteredCPCode(s.toString()); //Retrieve Filtered CustomerCodeList
CpListadapter.changeCursor(FilteredCPCodeList);
List Filtering works Perfect with codes above but when i click on a ListItem it's OnItemClickListener which use old Cursor cause an Exception which tells :
01-05 10:33:01.577: E/AndroidRuntime(5380): android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.
i know that change cursor will close my old cursor but i don't know how can i use StopManagingCursor on my old cursor(or another soloution) to solve this Issue.i've tried this code on onTextChanged() but it doesn't work either
Cursor OldCursor = CpListadapter.getCursor();
stopManagingCursor(OldCursor );
any help would be appreciated,Thanks
stopManagingCursor() is deprecated and not recommended anymore. You should be using CursorLoader. Then you can use a SimpleCursorAdapter along with the swapCursor(Cursor) method.
If you need to use your current setup, you should be able to do CpListadapter.getCursor().close() (for example, in your onDestroy()).

ListView SimpleCursorAdapter Update

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.

How to requery for a cursor now if requery is deprecated?

As per the title, if we used to call cursor.requery(), but it is now deprecated. How do you call this function now?
This method is deprecated.
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.
So how does one request a new cursor and pass it back to the adapter?
Re-initialize cursor when your any DML query execute.
See also this.
Just think about a block where you need to use cursor again and again. Populate it with the query. Work with it and then close before reusing
{
Cursor c =//Populating cursor again
while (c.moveToNext()) {
}
if (c != null) c.close();
}
Use a Loader to manage your cursors. It will return a new Cursor as needed and you can use swapCursor() on the adapter to refresh the data.
I used ListAdapter.notifyDataSetChanged(); this worked only on list (like ArrayList), when the listcontent changed.
Using databaseoutput, it doesn't work anymore. Therefore I took cursor.requery(); which is not only depricated. It also works only, if database entries changed.
But now, I want to change my view (switch between birthdays and dates) by changing my settings in a database, so requery does not recognize it.
For all changes works (a little bit dirty): this.onCreate(null);

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