When to use a Custom CursorLoader? - android

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.

Related

How to get custom SQLite DB table URI without using content provider?

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.

How to use LoaderManager.LoaderCallbacks and CursorLoader for ORMLite?

Recently I started using ORMLite for my application. Before I used to use 'ContentProvider' and implement LoaderManager.LoaderCallbacks<Cursor>. I would create CursorLoader and show the list to user when I have data.
How can I manage that cycle with ORMLite?
I want to create Cursor and get notified when Cursor is ready.
Thanks

Can I extends contentProvider without my own database?

Can I create a class extends ContentProvider without my own database? Instead, I return the cursor from other ContentProvider. Is it possible?
well, from the code, may be you can do that.
but i dont think it's a good idea, you can return the cursor from another content provider, why not directed to use the another content provider.
You can surely do that. However, you have to properly implement the ContentProvider. How you do that and where the Cursor comes from is quite irrelevant. Obviously I partially agree with #idiottiger where it might be simpler just to directly use the existing content provider.

Utterly confused about loading SQLite data in Honeycomb (Android)

I am new to android development and I've hit a hurdle when trying to load SQLite data to populate a ListFragment. In previous versions of android one made a new instance of the cursor class, made an SQLite query to place the cursor in the appropriate position, called startManagingCursor, made a new SimpleCursorAdapter and finally called setListAdapter. Pretty darn simple (too bad about the UI thread)!
Now almost all of these methods are deprecated and I have no idea how to populate my poor ListView. The documentation says I should use CursorLoader but here on StackOverflow people advise against using it for SQLite queries. How do I tell my cursor to populate the ListView?
Thanks a lot in advance!
You need to convert you DbAdapter into a Content Provider if you want to use CursorLoaders. and put android:exported= false as a property of your content provider so that it is private. Android team is favoring this approach as they say Content Provider is better suited to handle logic. That is why they are bent on deprecating our old ways( was hard on me too). But I have changed my dbadapters to content providers and now gleefully using cursor loaders( they are too cool not to be used). Try it, generally you will do fine with some more boilerplate code of Content Providers in adition to that of DBadapter and sqliteHelper. GO for it!

Is it possible to create a live folder with out a sql connection? (android api)

I've been reading http://developer.android.com/resources/articles/live-folders.html
and http://developer.android.com/reference/android/provider/LiveFolders.html
and https://android.googlesource.com/platform/packages/apps/Contacts/+/donut-release/src/com/android/contacts/ContactsLiveFolders.java
But I want to extend my app (Which is a listview) to a live folder. It would be a nice fit. How do i use my listadapter as a cursor? Is this possible? And past that, how do i set the similar getView that my Listadapter provides? is a Baseadapter, which my listadapter inherits, able to become a cursor, which a livefolder needs?
Yes, this is possible. But you need a ContentProvider. While a ContentProvider is usually backed by some sql database, it is not an requirement. You can get the data in the ContentProvider's query() method from any source, as long as you return it as cursor. If you dont have a database cursor, you can wrap your data in a MatrixCursor instead and return the MatrixCursor.
I have such an implementation of the query() method and it works by returning a MatrixCursor.
It is a common misunderstanding that ContentProvider need to be backed up by datebase queries.
It is better to understand the ContentProvider as a contract which specifies 4 different (CRUD) methods which take certain types of arguments and return certain types.
Additionally, you can have different queries inside these 4 methods and switch them depending on the Uri.
Used like that ContentProviders can use any datasource and perforn any operation on that datasource, as long as you adhere to the contract specifiied by the ContentProvider class. That makes them extremely powerful.

Categories

Resources