In my application I have a sqlite database.The columns in this database are :- id, data,date-added, mydescription.
And android content provider Media.Image.External has following columns in its database:- _id , _data , _title , _date-added ,....and so on.
I want that whenever application is launched, my database gets synced with content provider database(only id, data, and date-added columns). So If android content provider database has an addition of row, my database also adds that row and if any row is deleted, then my database also deletes that row.
So how to achieve this thing.
Thanks
You can use Application class.
It is a singleton and guaranteed to start before your any Activity.
Inside it query data with the help of Cursor Loader. If the content provider correctly implemented you will get new data for every update in the DB.
Related
I have created my own Sqlite database with all the columns that are there in Content Provider Media Image.
Now I want to save(add) all the rows (data) of Content Provider to my sqlite database.
So please help me as to which is the best memory effective and fastest way to do this.
I want to run this process in background using a service class.
Edit:-
Androids Content Provider Media/External/Image/Media Database Columns:-
_id , _data, _size, _display-name , etc.....
My presentation Layer to Display:-
_data, _size, _display-name , _custom-column
Custome Column I get after processing _data of each image, and this process is a bit time taking. So I want to do that in background and save it (mapping _custom-column with _id) so that whenever the application is launched I just present it to user.
I have multiple cursor loaders set up pointing to different URIs in my content provider and they work without any issues. Each loader is for a specific table in my database.
However I have now created a new URI in my content provider to execute a join query on my tables. I have created a new loader that points to the URI to be used for performing a join query but the loader doesn't update the UI.
If I close the app and then open it then the new data appears but after inserting new data in one activity and then returning to my main activity, the data hasn't updated. The loader isn't being called despite the database have new data inserted to it.
I am calling cursor.setNotificationUri(getContext().getContentResolver(), uri); for my new join URI.
Is there any particular reason why this isn't working? Like I said the loaders work fine for standard querying of individual tables but a loader with a join query doesn't.
Is it that a loader can only listen to a single table being updated hence why it can't listen to the join?
EDIT:
Some more information about the workflow...
Activity 1 reads data using a join query of the separate tables and displays to the UI
Activity 2 inserts data into separate tables to the database
Activity 1 launches Activity 2 which finishes after inserting data
Activity 1 has a loaders to update the UI. Loaders reading from individual tables works fine but a loader reading from a join query doesn't get called when data is updated. It is only called when the activity is created.
I Know this question was asked a long time ago but hopefully it will help some others experiencing the same problem... I had exactly the same issue
In your Query method in your Content Provider Class, before you set the notification Uri. Make sure the Update and Delete Uri's are the same as the query Uri's.
uri = Uri.parse("content://com.casselsinthecloud.cloudbeats.provider/playlist_contents");
Then call setNotificationUri(...) with the new Uri..
cursor.setNotificationUri(getContext().getContentResolver(), uri);
This will ensure the CursorLoader is updated dynamically..
In my Android application, I am using Sqlite DataBase to store the data from the server. I am using ContentProvider and ContentResolver to access the data from the DataBase and using CursorAdapter to bind the data to the ListView. As soon the data is inserted into the DataBase, CursorAdapter will be notified to update the ListView. Also, whenever I scroll the ListView, I get new data from the DataBase table and ListView will be updated. But once I reaches to the end of the table row, I need to fetch the data directly from the server without storing into the DataBase and update it in the ListView. Now as I am using CursorAdapter which accepts Cursor data, how can I bind a new set of data which is not coming from the DataBase? Is is possible to create Cursor data without getting data from the DataBase and use ChangeCursor() method in the ContentProvider to update the ListView? If not, is there any other method to achieve the same?
Is is possible to create Cursor data without getting data from the
DataBase and use ChangeCursor() method in the ContentAdapter to update
the ListView?
Yes, you can create cursor using MatrixCursor. If you will have to merge MatrixCursor with database Cursor use MergeCursor.
I have an app that uses an sqlite database. Currently I have a database helper class that I call from within my main class to access the database. I then temporarily store the items I retrieved in a cursor. Every time I want to access my database I have to open the db, create a new cursor, query the db and add the results to the cursor, pull the information out of the cursor, close the cursor, close the db, then display to the user.
Now I'm accessing the db helper class every time I want to access the db
I'm creating a new cursor every time
And I'm making sqlite queries.
I was wondering if I should change my code to do something like:
Open DB
Store all data to a cursor
close db
then whenever I want to access the information I can instead just reference the cursor eliminating the need to call the db helper class again.
Or would it be more efficient to instead store the data to an array doing something like:
open db
store data to a cursor
loop through cursor and store data into an array
close cursor
close db
What would the difference be (processing time, and memory use) between these and is there an even more efficient way of doing this that uses the least resources? Is it faster to use an array or a cursor? I don't want to waste cpu time calling unnecessary functions or use more memory storing bloated objects.
In your activity if you want to execute a select query once and you using that data to your activity then you have to make a ArrayList or Hashmap from Cursor and then you have to close your cursor.
But if you have to access a database many times and you are getting a different different results by executing a query then you have to use cursor and fetch data from cursor update to UI and close Cursor.
You are using Helper class so it is the best choice so you don't have to wonder about processing.
when ever you want to fetch data then just make a object of Helper class,Open connection,execute query and return data,fetch data from cursor in your activity,Update UI,close database.
this is the normal way and also it is a good way.
I got following problem, I need to use a Content Provider to read a
Database of an other App.
first I want all rows, and after analyzing the data only e.g. the rows
from _id = 1, 3 and 5.
how can I call a Content provider and select only these rows?
or is it possible to create a subset Cursor form an given Cursor?
Thanks in advance.
If you're talking to another app, I assume you're querying the other app's ContentProvider to get the data from them in the first place.
In this situation, the cleanest answer seems not to build your own ContentProvider that filters/wraps theirs. Instead query their ContentProvider from your application directly, and use the select clause in your query() to specify the conditions that define the subset of data you want to be given.