Content Resolver SQLite refresh data after operation - android

My problem is that I do content resolver operations (CRUD) and my local Sqlite data base gets the changes but they do not show/refresh those changes in my TEXTVIEW, in here I'm not talking about refreshing a listview/recyclerview with adapters like 100% of the questions in here I'm just talking about a simple text view which fetch information from the local database so in here I'm not using adapters or recycler views (not yet).
I've tried:
Use the database helper constructor after a new content resolver operation but is not working.
use the close() after a content provider operation.
cResolver.notifyChange() after applying a batch of operations.
I've tried to use the LoaderManager but it appears that is only useful for adapters with the swapcursor.
How do I know my CRUD ops are working? if I restart my app the changes are there. Any help in the rigth directions will be appreciated thanks.

Any change on your database is reflected only on your database. If you need to update a text view after the change, you need to handle that the change have been made and update your view.
An alternative to update your view and data after persisting it to a database is using the LiveData or any observable.

Related

How to refresh spinner from main activity after inserted to sqlite

I working on an aplication that uses rest to get data from mysql to sqlite database. On main activity, I have a spinner and after I synchronize data by pressing seckronize button, data inserted to sqlite but after that when I query data It can not catch result and returns empty cursor. Since cursor requery depracated which method should I use. Maybe loaders but when I try loaders I do not know how to trigger loaders if there is an update in database. I do not want to use content providers.
Can you help me?
Thanks
Check this out .Its a good tutorial on how to load in background with several useful callback methods

Any cursorLoader with out content provider that notyfies data change?

i am working on a project where i have recyclerview and i populate the recyclerview with data from database and i am using cursor loader to load the data and custom cursor adapter to set the data to recyclerview.I have not created any content provider because my app dose not share any information.But the problem is since i am not using any content provider cursorloader is not refreshing after the data change.I just want to know if there is any way to solve the problem or are there any custom cursorloaders that do the work..
Thank you
Well, you can use content providers without sharing the contents with other apps. Honestly I don't know why the documentation states that you only should use one if you want to share data. Thats nonsense considering that you get tons of neat features for free by simply using a content provider. And if you don't want to share any data with other apps, then set the 'exported' flag to false in the manifest file.
If you don't want to use a provider, then you can signal a change with:
[any Context].getContentResolver().notifyChange(youtUri, null)

Best practice for loose coupling between data & UI in Android - Adapter, Filter, CursorLoader and ContentProvider

Assume we have an Activity with n TextViews that represent one line notes. These notes are stored somewhere (local database, network etc), and each time onResume() being called, the proper number of TextViews are drawn according to that stored data.
Now, lets say the user want to delete a note, what would be the best way the resolve the specific TextView, back to its storage entity?
At the moment, the only way I know is by using View.Tag, and having some manager to translate it to data entity, but it look rather messy.
Are there any other options?
In Android, the Adapter acts a bridge between the view and the data model. You could display the n TextViews in either a ListView or a GridView, and when the user adds or deletes a note, the local or server database is first updated. Upon completion of the web service call and/or the local database update, the new data is added to the underlying Adapter. The View is then refreshed by calling adapter.notifyDataSetChanged(). This would be the way to do it.
Approaches:
If updating the local SQLite database, you could consider using a
CursorAdpater
to hold the data for the View, as it directly maps the entries in
the local database to the View.
If making use of a ContentProvider, it is even possible to combine
a CursorAdapter with a
LoaderManager
and a
CursorLoader:
these plug into the Activity / Fragment life-cycle and monitor
the underlying ContentProvider for changes that are published
automatically to the View on a separate thread.
It is also possible to use a
Filter
in conjunction with the Adapter to define a dynamic mechanism that
sorts the data entries on-the-fly. The filtering is performed by the
Filter on a separate thread, as per a query entered by the user,
possibly in an
AutoCompleteTextView.
References:
See the Retrieving a List of
Contacts
tutorial. The example here retrieves a set of contacts from the
contacts ContentProvider based on a dynamic, alphabetical search by
the user. It makes use of CursorAdapter, CursorLoader and
LoaderManager to monitor and update the data, and it displays the
search results in a ListView.
See also the Android Realtime (Instant) Search with Filter Class example, which shows how a Filter is to be used.
Android AutoCompleteTextView with Custom Adapter filtering.
Android AutocompleteTextView using ArrayAdapter and Filter.

Pagination with filtering using CursorLoader, AutocompleteTextView, CursorAdapter, ContentProvider in android

I have some trouble when using CursorLoader... I want to download data from inet API page by page, however;
I also want to make pagination of the listview. It means that data should be downloaded page by page when the end of listview is reached.
I also want to filter my listview inputing constraint-text in AutocompleteTextView.
Each of these features works properly when I use them separately, but when I want use them together it works not pretty well. I want to implement such a scenario: if I entered filter-text in AutocompleteTextView my listview was invalidated (that works fine) and downloading process would start until listview size reach the end of screen.
The problem is I don't know how to organize the cursor update through CursorLoader, when I should restart loader and when should't I? Should I restart loader only when I set filter (setFilterQueryProvider, method runQuery(CharSequence constraint)) or should I do it when give new portion of data from inet?
Now when process was started I found out that callback onLoadFinished wasn't called and listview wasn't updated...
Maybe anybody give me some working example...
You could change your CursorLoader for an AsyncTaskLoader to fetch new information when no records found according to the filter criteria. With the AsyncTaskLoader you could handle DB and UI operations to manage the Activity's state when it is downloading data or querying it locally.
Hope it helps.

Android: Using a Loader to make http request, update SQLite database & populate GridView fragment

In my Android app, I am making a http request, getting some data, putting it in my local sqlite database & then populating a gridview using that data. I have the code working for an activity but I need to use fragment now to get this done as there are many similar pages to be shown. I read that using Loader is the best way to deal with data in a fragment.
I am not sure about:
Whether to use CursorLoader, Async taskLoader or SQLiteLoader(developed by commons guy).
In which of the loader functions (onCreateLoader(), onLoadFinished() etc.) do I put my code for making http request, populating the local database & getting the data displayed in a gridview in my fragment
I am also using a lazyload list to show images. How will that fit into the entire thing if I use loader
Can anybody help me with this one? Tried searching for good examples or tutorials but I haven't really found something that's really useful. So, please suggest any if you can. Thanks
Whether to use CursorLoader, Async taskLoader or
SQLiteLoader(developed by commons guy).
CursorLoader is for ContentProviders(which is not your case) and AsyncTaskLoader is the way to go. I haven't use the classes from Commonsware but if they allow overriding of some of their methods then I guess you can use it.
In which of the loader functions (onCreateLoader(), onLoadFinished()
etc.) do I put my code for making http request, populating the local
database & getting the data displayed in a gridview in my fragment
In none of those callbacks because they run(most likely) on the main UI and you must not do network operations in there. The Loader subclasses have the loadInBackground method which runs on a background thread. On this method the Loader queries for data and in which you could place your networks requests and database updating. But you would need to be very careful to not insert duplicate data in the database.
I am also using a lazyload list to show images. How will that fit into
the entire thing if I use loader
As I haven't seen your code, I don't think this two parts are connected. I'm guessing that you use the lazy image loading code directly in the GridView's adapter.
My advice is to not use Loaders for loading and inserting data because their purpose is to only load data on a background thread(having taking care of configuration changes). For your particular situation I would make my own AsyncTaskLoader(or use Commonsware's library) which queries the database for new data. I would then start a new AsyncTask to do the http request and to insert data in the database and then I would trigger a Loader restart in the onPostExecute method of the AsyncTask(with getLoaderManager().restartLoader...). Have a look at this similar question for some problems related to what you're trying to do.

Categories

Resources