What a question!
To describe it more clearly:
I’m using fragments and my target is to have a TextView that shows the total sum of a database table column. This total sum should be updated everytime the table is updated in different ways (delete, insert, update).
Without the contentprovider I would write a method in my DBHelper class that would return the value. But the the problem begins.It’s really difficult (till impossible) in fragments to update the textview programmatically. Therefore I like to use the contentprovider because it always informs my listviews about changes without any additional work from my side. In this case (contentprovider) I have (imho) to use the loader methods onCreateLoader() and onLoadFinished() with a cursorloader and a uri. But they all are returning a cursor object and that’s nothing I can attach on this single textview.
After all the days working on listviews and contentproviders my thinking is maybe too “bigpictured” so that I’m not able to see a simple solution for a simple textview. If there is any…
What a question! To describe it more clearly: I’m using fragments and my target
Share
I found one, at least, workaround. Would appreciate if somebody knows a better solution.
In order to use Loader you have to override 3 methods:
So let’s say we have a single TextView tv that should be updated via ContentProvider.
in onCreateLoader():
MaterialContentProvider.READ_SINGLE_MATSTAMM_URI is my URI that shows the ContentProvider wich db table to use and how. In this case I’ll get a single result.
This we will get asynchroniosly via onLoadFinished() and here comes my workaround:
From now on your TextView tv will be updated everytime the content changes in the table that is targeted by the uri in onCreateLoader(). The “trick” is to not give away the cursor like you usually do (with something like “adapter.swap(cursor)”) because there is nothing with an adapter outthere. In order to get a total sum of a column you have to get all the rows of this special column with the cursor (wich means that you have a different uri).