How to refresh spinner from main activity after inserted to sqlite - android

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

Related

Content Resolver SQLite refresh data after operation

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.

Is it necessary to do SQLite local querys in an Asyntask?

I have a doubt... I want to execute this code, but I don't know if it's better to do it in an Asynctask or it isn't necessary and I can execute it in the main function.. What do you think?
The SQLite Database is local, so the acces is so fast.. Thank you!
/*Here I create an object of SQLiteHelper Class*/
SQLiteHelper bbDD;
bbDD = new SQLiteHelper(getActivity());
/* Calling this method I open the database connection */
bbDD.open();
/* This method returns me an ArrayList, doing a SELECT in the SQLite Local Database*/
arrayList = bbDD.selectAll();
/* Here I Close the database connection */
bbDD.close();
Today's devices are fast enough to handle SQLite query inside main UI thread, but when you try to query big size of data inside SQLite (for example if you have more than hundred rows of data inside that database (Especially if row contains base64 encoded image strings)) user can see some UI lag, that's why query in Asynctask makes UI faster - without lags. If you don't have big data inside SQLite, there is no need to use AsyncTask for query.
No dude there is no need to write SQLite local querys in an Asyntask.
The answer to your question depend on the size of your database.
I have experienced to select hundreds of rows, with lot of columns on SQLite. It does not make the apps very lag, but it does make the apps a little bit lag (and my client can feel this). So, i moved the select operation to the onBackground in the Async class, and the apps run flawlessly again.
If you only have a little data, theres no need to put it on the background.

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.

Downloading data in separate thread before initializing a ListView

I'm having issues with multithreading in my application. I know there are many posts on Threads/AsyncTasks/etc, but none seem to address my specific problem.
Basically, I get a query string in my search Activity, then send it to my results Activity, where the string is used as a SQL query, the results are returned as an array of JSON objects, then I display these objects in a ListView (which is part of the results Activity). All of my SQL connection and retrieval is done in a separate class that I call at the start of the results Activity.
MySQLRetrieve data = new MySQLRetrieve();
ArrayList<Tile> tiles = data.getResults(nameValuePairs, isLocationSearch);
The above code is how I get the SQL response and convert into an ArrayList, which I then use the populate my ListView with. getResults() takes care of all of this.
I already have separate threads working to download images into the ListView, but what I can't get to work is getting the SQL query and result to run in it's own Thread. What I want to achieve is this:
User enters search query in search Activity.
Intent is sent to results Activity, and it starts immediately.
ProgressDialog (just the animated spinner thing, not a loading bar) displays while the SQL query is taking place.
ListView populates with objects from the JSON array, lazy loading images as they come.
I have steps 1,2, and 4 working well, but 3 is the problem. I've looked up AsyncTasks, which seem to be the answer, but I just can't get them to work. Does anyone have a solution to this problem? I need to do this, so when starting the results Activity, the UI changes immediately to the results Activity and doesn't have to wait until the SQL response is returned.
And yes, I've already read the painless-threading post.
Thank you.
I would recommend against creating that ArrayList<Tile> to reduce memory consumption (and code size) and instead directly bind the SQLite Cursor to the ListView using a CursorAdapter.
That alone might just increase the performance enough that you don't need to do any async loading.
If you still want async loading, check out the LoaderManager framework (available since Android 3.0/ API level 11, with Android support package down to 1.6/4) which will automagically do asynchronous loading of your Cursor -- either using the built-in CursorLoader (if you happen to have a ContentProvider), or the SimpleCursorLoader created by a fellow SO user (if you don't).

Categories

Resources