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?
Asked
Active
Viewed 256 times
-1
-
any reason for not using `ContentProvider`? and if you really need it, see sources of `CursorLoader`... – pskink Aug 23 '15 at 18:27
-
@pskink just do not want the database to be a central repo to be accessed by external apps as well . Just want it to be private for particular app's use. Since do no want it to be accessible by other apps dats y dont wanna use contentprovider – user3819446 Aug 23 '15 at 18:29
-
make it "exported" = false then, `android:exported: Whether the content provider is available for other applications to use` – pskink Aug 23 '15 at 18:36
-
didn't get you . I mean if I use contentprovider then a constant defined in the manifest would be passed to the loader. but when i dont use the contentprovider how do i set database as the source to the loader? – user3819446 Aug 23 '15 at 18:38
-
possible duplicate of [Usage CursorLoader without ContentProvider](http://stackoverflow.com/questions/7182485/usage-cursorloader-without-contentprovider) – Selvin Aug 23 '15 at 19:02
1 Answers
0
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 anSQLiteDatabase
instead of aContentResolver
. 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.

Karakuri
- 38,365
- 12
- 84
- 104
-
so wud it automatically change the cursor content when content in the database changes ? – user3819446 Aug 23 '15 at 18:41
-
this is what i suggested to OP but still option 2 has little sense, if you can just use not exported `ContentProvider` – pskink Aug 23 '15 at 18:42
-
@user3819446 this is why you should use `ContentProvider`, i still dont see any reason to make custom `Loader` – pskink Aug 23 '15 at 18:43
-
-
@pskink Even if he uses a ContentProvider, he'll have to implement notification of the URI, because that's not automatic. The advantage of option 2 is that you aren't bound by the `ContentResolver` API and can access your database directly. There's also overhead in setting up a ContentProvider. – Karakuri Aug 23 '15 at 18:48
-
@Karakuri personaly i dont see any advantages of #2, since you either do a custom Loader per specific query in the specific table or make it generic `Loader` but then you would need some interface to pass database, query, query params etc which is `CursorLoader` already doing – pskink Aug 23 '15 at 18:53
-