什么CursorAdapter的(上下文的背景下,光标C,INT标志)设置,以便使其与CursorLoader工作? [英] What to set CursorAdapter(Context context, Cursor c, int flags) to in order to make it work with CursorLoader?

本文介绍了什么CursorAdapter的(上下文的背景下,光标C,INT标志)设置,以便使其与CursorLoader工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌文档指出,不使用 CursorAdapters 第一个构造函数,

The google docs point out not to use the CursorAdapters first constructor,

CursorAdapter(Context context, Cursor c)

有只有两个选项,

CursorAdapter(Context context, Cursor c, boolean autoRequery)

它说

构造,允许通过自动重新查询控制。建议您不要使用这个,而是CursorAdapter的(语境下,光标,INT)。当使用此构造,FLAG_REGISTER_CONTENT_OBSERVER总是会set.`

Constructor that allows control over auto-requery. It is recommended you not use this, but instead CursorAdapter(Context, Cursor, int). When using this constructor, FLAG_REGISTER_CONTENT_OBSERVER will always be set.`

CursorAdapter(Context context, Cursor c, int flags)`

这说,这是建议的构造。

which says it is the recommended constructor.

问题是,有在这里的最后一个构造函数只使用两个标志, FLAG_AUTO_REQUERY (INT 1)和 FLAG_REGISTER_CONTENT_OBSERVER (INT 2)。 使用 FLAG_AUTO_REQUERY ,因为我现在使用的其在后台进行管理和更新它CursorLoader没有意义。随着 FLAG_REGISTER_CONTENT_OBSERVER ,它说,它不是在需要的时候使用 CursorLoader

Problem is there are only two flags to use with the last constructor here, FLAG_AUTO_REQUERY(int 1) and FLAG_REGISTER_CONTENT_OBSERVER(int 2). Using FLAG_AUTO_REQUERY doesn't make sense because I am now using a CursorLoader in which to manage it in the background as well as update it. With FLAG_REGISTER_CONTENT_OBSERVER, it says its not needed when using CursorLoader.

现在我要问,什么整数做我通过的CursorAdapter(上下文的背景下,光标C,INT标志),以使其正常工作与我的的CursorAdapter ?请告诉我担心我是如何正确处理旧的光标。我真的不知道正确的方式来做到这一点。

Now I ask, what integer do I pass CursorAdapter(Context context, Cursor c, int flags) in order to make it work fine with my CursorAdapter? Whats worrying me is how to correctly manage the old cursor. I am not really sure the correct way to do this.

如果我用 FLAG_REGISTER_CONTENT_OBSERVER ,那么我必须做一些与 onContentChanged(),但在使用 swapCursor()在我的 LoaderManager ,因为光标未关闭,我只是做 adapter.swapCursor (光标).close()。但是会与 onContentChanged()的CursorAdapter 冲突?目标是不会造成任何内存泄漏和有效率的。

If I use FLAG_REGISTER_CONTENT_OBSERVER, then I must do something with onContentChanged(), but when using swapCursor() in my LoaderManager, since the cursor is not closed, I could just do adapter.swapCursor(cursor).close(). But would that conflict with onContentChanged() in CursorAdapter? Goal is to not cause any memory leaks and be efficient.

推荐答案

我的博客上讲述这个主题的几个星期前...也许读通过它将帮助。你也可以考虑通过<一读href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderCursor.html">sample对开发商的网站code 。

I blogged about this topic a couple weeks ago... maybe reading through it will help. You might also consider reading through the sample code on the developers site.

我应该使用哪种构造?

使用的CursorAdapter(上下文的背景下,光标C中,int标志)(文档推荐使用此构造上是前者)。

Use CursorAdapter(Context context, Cursor c, int flags) (the documentation recommends using this constructor over the former).

我通过什么整数的CursorAdapter(上下文的背景下,光标C中,int标志)

只是通过它的整数 0 。你不希望将它传递 FLAG_REGISTER_CONTENT_OBSERVER ,因为你使用的是 CursorLoader CursorAdapter的(因为 CursorLoader 注册 ContentObserver你),你肯定不希望将它传递 FLAG_AUTO_REQUERY`因为这标志德precated。

Just pass it the integer 0. You don't want to pass it FLAG_REGISTER_CONTENT_OBSERVER, since you are using a CursorLoader with your CursorAdapter (since the CursorLoader registers the ContentObserver for you), and you definitely don't want to pass itFLAG_AUTO_REQUERY` since that flag is deprecated.

什么是担心我是如何正确处理旧的光标。我真的不知道正确的方式来做到这一点。

What's worrying me is how to correctly manage the old cursor. I am not really sure the correct way to do this.

整点的 LoaderManager 的是,它所有的恼人的光标管理的东西给你,在幕后。具有自动加载的数据,而不必担心管理查询光标的方便是precisely为什么老 startManagingCursor stopManagingCursor 方法是去precated。

The whole point of the LoaderManager is that it does all of the annoying cursor management stuff for you, behind the scenes. The convenience of having your data loaded automatically without having to worry about managing the queried cursor is precisely why the old startManagingCursor and stopManagingCursor methods were deprecated.

...我只是做 adapter.swapCursor(光标).close()

不要那样做。该 LoaderManager 将关闭自己光标。事实上,如果我没记错的话,你会如果你尝试调用关闭()上的光标会得到一个错误。这听起来像你不应该重写 onContentChanged()或者

Don't do that. The LoaderManager will close the cursor on its own. In fact, if I remember correctly, you will get an error if you attempt to call close() on the cursor. It sounds like you shouldn't have to override onContentChanged() either.

这篇关于什么CursorAdapter的(上下文的背景下,光标C,INT标志)设置,以便使其与CursorLoader工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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