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.
Related
I am spinning my head around this theme for quite some time. I don't have much experience with Android and I'm trying to code everything using the best approach I can.
I successfuly created a Content Provider to wrap my SQLite and some Loaders to query the Data. It works fine, but I'm having some problems deciding how to insert, delete and update outside of the main Thread.
I implemented various AsyncTasks to manage these operations and the results seems nice.
But is this the right approach or am I missing something here?
Is there any special class or pattern out there that I never heard of?
It would be nice if anybody could point me to some direction.
Tks
You can use an AsyncQueryHandler. Pretty much exactly what you've done using AsyncTask, but all the heavy lifting is already dealt with.
Assuming your Content Provider is all good, basically all you really need to do is (for an update):
AsyncQueryHandler handler = new AsyncQueryHandler(ctx.getContentResolver());
final ContentValues cv = new ContentValues();
values.put(column, data);
handler.startUpdate(-1, null, uriOfDatabase, cv, selection, selectionArgs);
See the documentation here
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.
When I use SQLite to bring my data and use cursor and adapters, should I use cursor Loaders??. Is this the best practice?. Im not quite clear when to use cursor loaders. Should I use it only if my app shares data with other apps?. My question comes because it have been really annoying for me using cursors + adapters + listView; sometimes the notifyDataSetChanged works, sometimes not, so it have been really tricky sometimes. I start reading about cursor loaders but Im not sure if this is a work around for this in particular or if I can use it as a work around.
Any clarification will be really appreciated!!
Thanks guys.
Use Loaders to ensure that all cursor operations are done asynchronously, thus eliminating the possibility of blocking the UI thread.
When using CursorAdapter donĀ“t use notifyDataSetChanged instead use:
db.updateData();
yourCursor = db.getData();
yourAdapter.changeCursor(yourCursor);
I am in a situation where I will probably need to manipulate two separate Cursor objects. I was hoping there was someway to create our own Cursor by combining two separate ones. But I am not sure if it is even allowed. Read somewhere you can't, but I wanted to devote a specific question to that so it is at least given some attention for others who have a similar situation.
I was hoping there was someway to create our own Cursor by combining two separate ones
Have you tried MergeCursor? Quoting the documentation:
A convience class that lets you present an array of Cursors as a single linear Cursor. The schema of the cursors presented is entirely up to the creator of the MergeCursor, and may be different if that is desired. Calls to getColumns, getColumnIndex, etc will return the value for the row that the MergeCursor is currently pointing at.
Also, if your objective is to display both Cursors in some form of AdapterView, you could use my MergeAdapter and concatenate things at the Adapter level
Even if you found a solution for your concrete use case, i would still like to answer your main question.
Yes it's possible to implement your own cursors.
Cursor is a public interface and can be implemented by everybody.
It has, however, a large amount of methods that need to be implemented.
Using AbstractCursor or AbstractWindowedCursor as base class can help to reduce the overhead.
The available concrete implementations can be extended, too.
CrossProcessCursor, CrossProcessCursorWrapper, CursorWrapper, MatrixCursor, MergeCursor, MockCursor, SQLiteCursor
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!