带有 LoaderManager 的 CursorLoader 如何知道将游标发送到 CursorAdapter? [英] How does CursorLoader with LoaderManager know to send the cursor to a CursorAdapter?
问题描述
我正在浏览我的一些代码,但我意识到我实际上并不知道如何将 CursorLoader
和 LoaderManager
与 CursorAdapter
组合一起使用> 连接.这是我感到困惑的部分.
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);
那么来自我的自定义 ContentProvider
的 query()
方法如何知道将其发送到特定的 CursorAdapter
?我只是看不到联系.我理解其中的所有其他内容,但这个问题是关于什么的.哦,我应该提一下,代码运行良好.
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...
我的自定义 ContentProvider
中的 query() 方法如何...?
How does the query() method from my custom
ContentProvider
...?
好吧,首先要记住 getContentResolver().query()
不会直接调用内容提供者的 query
方法.您正在调用内容解析器的查询方法,该方法解析 Uri
,确定您希望调用的提供者,然后然后调用您的提供者的 query
方法.
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.
如何将查询发送到特定的 CursorAdapter
?
How does the query get sent to that specific
CursorAdapter
?
我将使用API 演示 为例.请注意,API 演示使用 ListFragment
而不是 ListActivity
(区别在此问题的上下文中并不重要).
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).
首先,创建(并设置)
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);
执行此语句后,SimpleCursorAdapter
知道它应该如何将游标数据与您的视图相关联.光标的 Contacts.DISPLAY_NAME
列中的任何数据都将与具有 id android.R.id.text1
等的视图相关联.
After this statement is executed, the SimpleCursorAdapter
knows how it should associate the cursor data with your views. Whatever data is in the cursor's Contacts.DISPLAY_NAME
column will be associated with the view with id android.R.id.text1
, etc.
请注意,您已将 null
游标作为第三个参数传递给构造函数.这非常重要,因为我们还没有查询任何数据(这是LoaderManager
和CursorLoader
的工作).
Note that you have passed a null
cursor as the third argument to the constructor. This is very important, as we have not queried any data yet (this is the LoaderManager
and CursorLoader
's job).
接下来,初始化加载器.
getLoaderManager().initLoader(0, null, this);
这告诉LoaderManager
创建并启动对应id 0
的Loader
.
This tells the LoaderManager
to create and start the Loader
corresponding to id 0
.
LoaderManager
调用 onCreateLoader(int id, Bundle args)
.
The LoaderManager
calls onCreateLoader(int id, Bundle args)
.
onCreateloader
返回 Loader
接口的子类(即,在本例中为 CursorLoader
).此 CursorLoader
将执行初始查询,并在数据更改时自行更新.
onCreateloader
returns a subclass of the Loader<Cursor>
interface (i.e. a CursorLoader
, in this case). This CursorLoader
will perform the initial query and will update itself when the data changes.
如果您的 Activity/Fragment 有多个加载器,那么您可以使用 switch(id)
来确定已被指示开始加载过程的特定加载器.
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.
被查询的游标被传递给onLoadFinished()
.
在 CursorLoader
实例化并在步骤 3 中返回后,CursorLoader
立即在单独的线程上执行初始查询并返回一个游标.当 CursorLoader
完成查询时,它将新查询的游标返回给 LoaderManager
,然后将游标传递给 onLoadFinished
方法.从文档中可以看出,onLoadFinished
方法在之前创建的加载器完成加载时被调用."
Immediately after the CursorLoader
is instantiated and returned in step 3, the CursorLoader
performs the initial query on a separate thread and a cursor is returned. When the CursorLoader
finishes the query, it returns the newly queried cursor to the LoaderManager
, which then passes the cursor to the onLoadFinished
method. From the documentation, "the onLoadFinished
method is called when a previously created loader has finished its load."
查询的数据与CursorAdapter
相关联.
The queried data is associated with the CursorAdapter
.
mAdapter.swapCursor(data);
请注意,onLoadFinished
通常也是您使用查询数据更新活动/片段的 UI 的地方.在这种情况下这不是必需的,因为我们之前已经调用了 setListAdapter(mAdapter)
.ListFragment
知道如何使用 CursorAdapter
(参见第 1 步)...我们需要做的就是使用 swapCursor
将光标传递给适配器,而 ListFragment
将负责为我们在屏幕上显示数据.
Note that onLoadFinished
is 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 called setListAdapter(mAdapter)
. The ListFragment
knows how to use the CursorAdapter
(see step 1)... all we need to do is pass the adapter the cursor with swapCursor
, and the ListFragment
will 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.).
包含查询数据的游标与onLoadFinished
中的CursorAdapter
相关联.这通常通过调用 mAdapter.swapCursor(data)
来完成.
The cursor that contains your queried data is associated with the CursorAdapter
in onLoadFinished
. This is typically done by calling mAdapter.swapCursor(data)
.
这篇关于带有 LoaderManager 的 CursorLoader 如何知道将游标发送到 CursorAdapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!