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.
Related
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 have a ListView with attached custom CursorAdapter. Within the CursorAdapter bindView method I run some calculations that depend on user’s chosen display units (these can be changed at any time in Settings)
Normally to update a ListView I would run:
myCursor = DB.getData();
myDataAdapter.changeCursor(myCursor);
This works fine in most cases. But if user changed their display units in Settings, above code doesn’t update the ListView. I assume this is because data in the cursor hasn’t actually changed.
I tried running notifyDataSetInvalidated() and notifyDataSetChanged() against the adapter, also invalidateViews() against the ListView, but none of this make any difference.
What I currently do is recreate the adapter:
myCursor = db.getData();
myCursorAdapter = new myCursorAdapter(getActivity(),myCursor,0);
myListView.setAdapter(myCursorAdapter);
This works, but I don’t think it’s a good practice to re-create the adapter every time I need to refresh ListView... Is there a better way to do this?
EDIT:
myDataAdapter.changeCursor(myCursor) is actually working even when data in cursor hasn't changed. It was my own mistake in the Custom Cursor Adapter code that caused listview not to update unless adaptor was re-created.
notifyDataSetChanged() still doesn't work, but decided not to look into this further and just use changeCursor
The BaseAdapter method notifyDataSetChanged() should do what you want. It could be that the adapter you are passing in is not the same as the adapter the ListView is using. Try using getAdapter() instead:
((MyCursorAdapter) myListView.getAdapter()).notifyDataSetChanged();
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.
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.
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.