如何CursorLoader与LoaderManager知道送光标到CursorAdapter的? [英] How does CursorLoader with LoaderManager know to send the cursor to a CursorAdapter?

查看:142
本文介绍了如何CursorLoader与LoaderManager知道送光标到CursorAdapter的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过我的一些code,我意识到我真的不知道如何 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 知道它发送到特定的CursorAdapter ?我只是不明白的连接。我明白了一切在这一点,但什么这个问题上。哦,我应该提到,在code正常工作。

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 ...

How does the query() method from my custom ContentProvider...?

嗯,首先要记住, getContentResolver()查询()不调用内容提供商的查询方法直。要调用的内容解析的查询方法,该方法解析乌里,决定要调用提供者,而的调用您的供应商的查询方法

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).

  1. 首先,创建(和设置)的的CursorAdapter

  1. 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);

在执行该语句时, 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.

请注意,你已经通过了光标作为第三个参数的构造函数。这是的很重要的,因为我们还没有查询任何数据(这是 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

This tells the LoaderManager to create and start the Loader corresponding to id 0.

LoaderManager 要求 onCreateLoader(INT ID,捆绑参数)

The LoaderManager calls onCreateLoader(int id, Bundle args).

onCreateloader 返回装载机℃的子类,光标&GT; 接口(即 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.

如果你的活动/片段有一个以上的装载机,那么你会使用开关(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()

The queried cursor is passed to onLoadFinished().

紧随 CursorLoader 被实例化,并在第3步回来了, CursorLoader 执行在一个单独的初始查询后,螺纹和光标返回。当 CursorLoader 完成查询,它返回新查询光标移到 LoaderManager ,然后通过光标移到 onLoadFinished 方法。从文档的时候,pviously创建装载器的$ P $已经完成它的负载 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。这是没有必要在这种情况下,我们有previously名为 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.).

这包含您的查询的数据光标与的CursorAdapter onLoadFinished 相关联。这通常是通过调用完成 mAdapter.swapCursor(数据)

The cursor that contains your queried data is associated with the CursorAdapter in onLoadFinished. This is typically done by calling mAdapter.swapCursor(data).

这篇关于如何CursorLoader与LoaderManager知道送光标到CursorAdapter的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆