how to set the data source for a custom loader if not using content provider?
I need to populate a listview from a sqlite but do not want to create the content provider for the database, instead want to use loader to fetch data from the database. So how do I set sqlitedatabase as the data source of a custom loader?
Two options:
Use a ContentProvider and set android:exported="false" for the <provider> entry in your AndroidManifest.xml.
Copy the source code for CursorLoader and modify it to query an SQLiteDatabase instead of a ContentResolver. Then you only need to pass it a reference to your database.
Note that in order for the loader to requery, you need to properly implement data change notifications when you modify data in the database. Typically this is done using contentResolver.notifyChange() on a URI. For option 1, it should be the URI you gave to the CursorLoader; for option 2, you can manually set up a ContentObserver in your loader and do the same sort of notification, or you could implement some other signal to the loader to issue a requery.
Related
I want to load data from database while scrolling, so I am using loader callback. But in init loader it is asking for database URI. If I use a ContentProvider, then it is working fine. But I would like to initialize the loader without a ContentProvider.
You need to use a different Loader. I recommend copying the source of CursorLoader and changing it to use an instance of your database (more specifically, your subclass of SQLiteOpenHelper) instead of the Uri and other arguments it uses to talk to ContentResolver.
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)
I'm reading about cursor loaders, and I want to use them to observe changes on my sqlite db and for future use with GCM. I want to ask a simple question, should I make a cursor loader for each URI that I use in my Content provider, or there is a way to make only one general loader that will observe and change on any URI and update the corresponding activity?
To make my question specific to my case, I'll explain what i want to do. I'm making a social media app, so I have a list of feeds list view of which each list item will contain another list view that represents the comments. so I think I'll have two cursor adapters and two loaders, is that right?
In my app I want to show a list of apps that I have placed in an resource file.
I parse the XML(resource) file and then save the value in a SQLiteDatabase. I have implemented my Database inside a ContentProvider. What I want to know is do I need a Custom CursorLoader (should I extend CursorLoader?)? or will CursorLoader itself will be sufficient. I have seen an example, but in this no ContentProvider has been used.
Can someone explain when should I implement a Custom CursorLoader as against using the original one?
(A little unrelated) Also what would be the best practice, to implement a database with or without a ContentProvider?
Thanks in Advance!
There are many ways to implement it -
If using a ContentProvider there is no need to extend the CursorLoader.
If not using a ContentProvider and using a SQLiteDatabase instead, we can extend the CursorLoader with our Custom-Loader and override the loadInBackground() method of CursorLoader and instead of querying the ContentProvider we can query the SQLiteDatabase.
While using a SQLiteDatabase we can extend AsyncTaskLoader, however, this is more tedious method than one specified in 2.
I have created a SQLite DB (which I am trying to use as contentProvider) and need your help to guide me on this:
1) Register a content observer (I don't know how to create URI for my Database)
2) on DB changes like update/delete row, I want to notify the contentObserver which has registered to URI of DB .
How can this be done?
You need to extend the ContentProvider class to implement your content provider.
The Android documentation walks you through the necessary steps including how to signal ContentObservers:
http://developer.android.com/guide/topics/providers/content-providers.html