Android LoaderManager ContentProvider and loading data over network - android

I've searched StackOverflow and googled but I can't find an answer.
I'm trying to build a REST-Service using Virgil Dobjanschis Designpattern B ( http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html)
I have an activity with a SimpleCursorAdapter and a LoaderManager to query a ContentProvider. This works for data in my database. Data is re-queried and UI is getting refreshed.
After every query on the ContentProvider a Thread is started to sync the requested data from network, it loads json-objects, parses and save new data to database - everything fine, but the UI is not updated.
I think I have to send my Activity a message to refresh data, but I don't know how and can't find an example how to do that.
So, what is the right way to manage this?
---- Updated ----
I think I didn't explain my problem clearly.
As you can see in this pic:
Step 7 is working. A Helper-class is updating data.
Step 7' does not happen. I thought this would be a automatic feature of a ContentProvider.
My Helper-class (Resthandler) is updating the database directly without using ContentProvider.

You have a SimpleCursorAdapter, which I suppose you use to bind data from the content provider to your UI, correct? If that's true, then you need to move the retrieved Cursor to the cursor adapter every time you update the cursor.
Do this in CursorLoader.onLoadFinished(), by calling either
SimpleCursorAdapter.swapCursor() or SimpleCursorAdapter.changeCursor().
See the Android training class
Loading Data in the Background for a complete example.

I've found a solution for this. I overloaded insert() in my ContentProvider. So I can give ContentValues to it and CP is writing data to db. CP sends notifications to Views.
It is tested and working. I have found this in a book "Programming Android".

Related

Should I use a Cursor or a CursorLoader?

I have an android app in which I have a login system and some other stuff that communicate with the server. Sometimes I just get from the web server just a confirmation and sometimes I get a lot of data. So far I was using a simple database. Today I implemented a Content Provider which is working so far. To get data from the ContentProvider I used this Cursor cursor = getContentResolver().query();, but I saw that there is also the option to use a CursorLoader. What is the difference between them ? In my case what should I use ? I also saw that I have to implement it in every class the cursorLoader, can't I make a single class for it and call it when it's needed ?
As the documentation states,
CursorLoader implements the Loader protocol in a standard way for
querying cursors, building on AsyncTaskLoader to perform the cursor
query on a background thread so that it does not block the
application's UI.
This is the biggest advantage of using Loaders, i.e. it is asynchronous. Some of the other important advantages are also mentioned here.
They provide asynchronous loading of data.
They monitor the source of their data and deliver new results when the content changes.
They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data.
If you use the default cursors by querying the content provider directly then you need to handle closing them, and as you said you have huge data, you'd have to run the query code on a different thread. For all these purposes using CursorLoaders is much simpler and efficient. For code on how to use one, check this out.
As to your second question,
I also saw that I have to implement it in every class the
cursorLoader, can't I make a single class for it and call it when it's
needed ?
You can very well make a Base class that will be implementing the loader callbacks and then you can inherit that base class from all the classes that need to use the CursorLoaders.

How to efficiently update and get data from a SQLite database on Android?

I am making an app which gets a time schedule from a website and display it in my app in a nice ListView. To make the schedule available when the user is offline I am trying to save it in a SQLite database first and get the data from there. The idea is that the database gets updated when the user starts the app, when the user presses a refresh button and at set times by a service running in the background.
My question is: how can I efficiently update and get data from the database without using the UI thread, thus not blocking user interaction.
I think I should use a system which puts tasks in a queue so that the database gets updated before the data gets displayed in my ListView. Other than this I don't have a single clue how to realise such a system on Android.
Ok so you need to implement three things:
1) Get data from website to db: for this you should use an AsyncTask
2) Make a ListView that will get data from the db and update automatically. For this you will need to use a Loader which will handle the loading of the data on the background.
3) To use the loader you will need a ContentProvider to provide you with the cursor needed and notify the ListView on changes.
For all three there are many nice tutorials online, for example see here

Do I REALLY need to use ContentProvider?

If I do need it, I'll have to modify about 15 classes (models and model-manager classes), so I really want to know if I need a ContentProvider.
Here's where I am:
Similar to Twitter, I'm getting rows of data from a server, and saving it locally in case the user has no Internet connection. But the ideal way is to always get it from the Server.
I am probably not going to use SimpleCursorAdapter because the rows of data I get from the server includes URLs, which means I have to create a custom adapter to display images.
I need to load data into the ListViews asynchronously because I'm having a ViewPager with 3 Fragments that shows the same data (different filters tho), so, since a ViewPager loads 3 Fragments into memory, it means 3 queries are executing (and that's most likely the cause of non-smooth swiping).
So far, the way I synchronize data between the App and the Server is:
Fragment.onStart() executes an AsyncTask which returns rows of data formatted as JSON data
Said AsycTask.onPostExecute() updates the List<E> and calls Adapter.notifyDataSetChanged()
The issue here is that each time I change tabs, the onStart() is called, ergo the AsyncTask executes causing the UI not being smooth.
Should I change the way I synchronize data with the Server, or should I use ContentProvider?
EDIT: as a head up, the reason I'm asking is that startManagingCursor() method is depracated. It says to use the Loader framework, but it seems it's only available through ContentProvider
You don't need to develop your own provider if you don't intend to
share your data with other applications. However, you do need your own
provider to provide custom search suggestions in your own application.
You also need your own provider if you want to copy and paste complex
data or files from your application to other applications.
from http://developer.android.com/guide/topics/providers/content-providers.html
I wrote a custom CursorLoader based on the SimpleCursorLoader source code that comes within the support library. You can search this site for more information about writing a custom one.

is there any delay time to insert data using Ormlite

I have an Android app and in my login activity I get my data from a webservice and insert it into sqlite database and in my main activity I select this data from the database.
My problem is the first time I use use the app the list will be empty
I debugged the app and when I keep small amount of time between the login and the main activity , it works well but when I run it fast the list will be empty
I supposed that there is a delay from Ormlite library , I don't know maybe I am wrong
How can I solve this problem ?
Note: I used Async task to read data from the web-service and in the post execute method I inserted the data into the database then I called my main activity using intent
I supposed that there is a delay from Ormlite library , I don't know maybe I am wrong
The amount of time that it will take ORMLite to update the database is directly proportional to the amount of data being changed. If you are adding a large number of rows then you should consider doing it as a batch using the Dao.callBatchTasks(...) method.
See here for more details: ORMLite's createOrUpdate seems slow - what is normal speed?
DB operations especially multiple one take there time. That is also somewhere stated in the ORM documentation. So it is the right way to use AsyncTask to do this. I would argue to fill the database not on the post execute method but in the doInBackground method like your WebService call. Ensure to use a WeakReference to the ORM-DB-Connector. Furthermore, give the user a hint about the progress using a combination of Handler and ProgressDialog.

Is it possible to modify the Contacts using URI and cursorLoader instead of ContentResolver() insert() , delete() modfiy() etc

I am new to Android development and trying to add an activity that would let me manipulate the contacts without going to the contacts app.
I used a cursorLoader() to access the the Contacts and displayed it in a listView.
Now I want to insert, delete or edit a new contact .
My question is do I have to use ContentResolver().insert() for this or is there a way to do it using CursorLoader() itself. Is CursorLoader() just for accessing data ?
I do see examples to do this using ContentResolver() but wondering if that is not recommended anymore ?
Please let me know.
CursorLoader is indeed intended for just accessing data.
It's implementation helps you keep the data in your activity/fragment updates during it entire life cycle (it knows when to stop, restart, start etc.).
The insert command is an atomic command, there's no reason to wrap it in a Loader, it's a bit overhead for this simple action (but still needs to be executed in a new task)

Categories

Resources