Android Honeycomb 中的加载器
我试图弄清楚如何在 Android 3.0 中使用 Loaders
但似乎无法让它工作。文档仅描述了如何使用 CursorLoader,但我使用的是 AsyncTaskLoader。
从文档来看,您似乎只需要实现 AsyncTaskLoader.loadInBackground()
但在 getLoaderManager().initLoader()
之后永远不会调用它,然后在中创建加载器回调。
我可以看到调试消息显示 Created new loader LoaderInfo{4040a828 #0 : ArticleDataLoader{4036b350}}
所以看起来它已成功创建。
SDK 中的加载器当前是否有可能损坏,或者创建加载器后是否需要调用某些方法? (他们在 CursorLoader 示例中没有这样做)。
编辑:似乎在从 initLoader()
返回的 Loader 上调用 forceLoad()
至少会启动加载,但这意味着您无法正确处理旋转:(
I'm trying to figure out how to use Loaders
in Android 3.0 but can't seem to get it to work. The docs only describe using CursorLoader
but I'm using AsyncTaskLoader
.
From the docs it seems that you should only need to implement AsyncTaskLoader.loadInBackground()
but it never gets called after getLoaderManager().initLoader()
and then creating the loader in the callback.
I can see debug messages saying Created new loader LoaderInfo{4040a828 #0 : ArticleDataLoader{4036b350}}
so it seems like it is created successfully.
Is it possible that loaders are currently broken in the SDK or is there some method you need to call after creating the loader? (they haven't done that in the CursorLoader
example).
EDIT: Seems like calling forceLoad()
on the Loader returned from initLoader()
starts the loading at least but this means you can't handle rotations correctly :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Dianne Hackborn 回复了错误跟踪器,并向我们推荐了静态库实现。 CursorLoader 正在执行forceLoad(),这就是它工作的原因。
请参阅我附加的类,了解在错误跟踪器中在大多数简单情况下为您处理此问题的类:http://code.google.com/p/android/issues/detail?id=14944
Dianne Hackborn replied on the bug tracker and referred us to the static library implementation. CursorLoader is doing forceLoad() which is why it is working.
See my attached class for a class which handles this for you in most simple cases at the bug tracker: http://code.google.com/p/android/issues/detail?id=14944
您需要重写 onStartLoading() 方法。请查看开发人员网站上的示例。
You need to override the onStartLoading() method. Look at the example on the developer website.
亚历克斯;
您是否尝试验证 onLoadInBackground () 是否被调用?
onLoadInBackground():在工作线程上调用以执行实际加载。实现不应直接传递结果,而应从此方法返回结果,这最终将在 UI 线程上调用 DeliverResult(D)。如果实现需要在 UI 线程上处理结果,它们可以重写 DeliverResult(D) 并在那里执行此操作。
Alex;
Did you try to validate if the onLoadInBackground () gets even called?
onLoadInBackground (): Called on a worker thread to perform the actual load. Implementations should not deliver the result directly, but should return them from this method, which will eventually end up calling deliverResult(D) on the UI thread. If implementations need to process the results on the UI thread they may override deliverResult(D) and do so there.