I was going through some of my code and I realized I don't actually know how a CursorLoader and LoaderManager combination used with a CursorAdapter connect. Heres the part that I am confused in.
agendaAdapter = new MyAgendaAdapter(this, null);
makeProviderBundle(new String[] {"_id", "event_name", "start_date", "start_time",
"end_date", "end_time", "location"}, "date(?) >= start_date and date(?) <= end_date",
new String[]{getChosenDate(), getChosenDate()}, null);
getLoaderManager().initLoader(0, myBundle, MainDisplayActivity.this);
list.setAdapter(agendaAdapter);
So how does the query() method from my custom ContentProvider know to send it to that specific CursorAdapter? I just don't see the connection. I understand everything else in that but what this question is on. Oh and I should mention, the code works fine.
First of all, check out the code sample at this post and this post for an even more in-depth look into how the process works.
And now, to answer your questions...
How does the query() method from my custom
ContentProvider...?
Well, first remember that getContentResolver().query() doesn't invoke the content provider's query method directly. You are invoking the content resolver's query method, which parses the Uri, determines the provider you wish to invoke, and then calls your provider's query method.
How does the query get sent to that specific
CursorAdapter?
I'll walk you through the process using the API Demos as an example. Note that the API demos uses a ListFragment instead of a ListActivity (the difference is not important in the context of this question).
First, create (and set up) the
CursorAdapter.mAdapter = new SimpleCursorAdapter( getActivity(), android.R.layout.simple_list_item_2, null, new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS }, new int[] { android.R.id.text1, android.R.id.text2 }, 0);After this statement is executed, the
SimpleCursorAdapterknows how it should associate the cursor data with your views. Whatever data is in the cursor'sContacts.DISPLAY_NAMEcolumn will be associated with the view with idandroid.R.id.text1, etc.Note that you have passed a
nullcursor as the third argument to the constructor. This is very important, as we have not queried any data yet (this is theLoaderManagerandCursorLoader's job).Next, initialize the loader.
getLoaderManager().initLoader(0, null, this);This tells the
LoaderManagerto create and start theLoadercorresponding to id0.The
LoaderManagercallsonCreateLoader(int id, Bundle args).onCreateloaderreturns a subclass of theLoader<Cursor>interface (i.e. aCursorLoader, in this case). ThisCursorLoaderwill perform the initial query and will update itself when the data changes.If your activity/fragment has more than one loader, then you'd use
switch(id)to determine the specific loader that has been instructed to begin the loading process.The queried cursor is passed to
onLoadFinished().Immediately after the
CursorLoaderis instantiated and returned in step 3, theCursorLoaderperforms the initial query on a separate thread and a cursor is returned. When theCursorLoaderfinishes the query, it returns the newly queried cursor to theLoaderManager, which then passes the cursor to theonLoadFinishedmethod. From the documentation, "theonLoadFinishedmethod is called when a previously created loader has finished its load."The queried data is associated with the
CursorAdapter.mAdapter.swapCursor(data);Note that
onLoadFinishedis also typically where you update the activity/fragment's UI with the queried data. This isn't necessary in this case, as we have previously calledsetListAdapter(mAdapter). TheListFragmentknows how to use theCursorAdapter(see step 1)... all we need to do is pass the adapter the cursor withswapCursor, and theListFragmentwill take care of displaying the data on the screen for us.
Let me know if you have any questions (or if there are any typos, etc.).
TL;DR
The cursor that contains your queried data is associated with the CursorAdapter in onLoadFinished. This is typically done by calling mAdapter.swapCursor(data).
来源:https://stackoverflow.com/questions/11150527/how-does-cursorloader-with-loadermanager-know-to-send-the-cursor-to-a-cursoradap