在 android 2.2 中使用光标适配器和内容提供程序的正确方法是什么

发布于 2024-12-07 12:31:31 字数 1383 浏览 1 评论 0原文

我很困惑,我需要你的帮助。 我尝试遵循 Virgil Doobjanschi 在他的讲座中给出的说明 '开发 Android REST 客户端应用程序' 在 Google IO 2010 上给出。不幸的是,我找不到在 Content Provider 和 Cursor Adapter 之间实现有效通信的方法。

我这里遇到的问题与光标适配器有关,因此我们假设内容提供程序一切正常。例如,让我们尝试使用 Contacts ContentProvider 而不是我自己的。我尝试了最简单的解决方案 - 任何 ContentProvider(假设为联系人,由 SDK 提供)和 SimpleCursorAdapter。问题是包含联系人中光标的 SimpleCursorAdapter 的构造函数已被弃用。文件说:

此构造函数已弃用。

不鼓励使用此选项,因为它会导致在应用程序的 UI 线程上执行游标查询,从而导致响应能力差甚至应用程序无响应错误。作为替代方案,将 LoaderManager 与 CursorLoader 一起使用。

我的想法是:“好吧,我不会使用它。我会尝试使用 LoaderManager 和 CursorLoader,因为他们建议我。”所以我去了 LoaderManager 文档站点 查找使用示例我发现了什么?使用 SimpleCursorAdapter 构造函数的完美示例。是的,我想避免同样的情况,因为它已被弃用。

    // Create an empty adapter we will use to display the loaded data.
    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);
    setListAdapter(mAdapter);

我能找到的所有教程都使用这个已弃用的构造函数。谁能给我提供好的答案,避免使用这个的正确方法是什么?又或许是我太在意它了?我想要的只是学习良好的实践......

i'm confused and i need your help.
I try to follow the instructions given by Virgil Dobjanschi on his lecture 'Developing Android REST Client Applications' given on Google IO 2010. Unfortunately, i can't find the way to implement valid communication between Content Provider and Cursor Adapter.

The problem i have here is linked with cursor adapter, so let's just assume everything is fine with the content provider. For example, let's try using Contacts ContentProvider instead of my own. I tried the simplest solution - any ContentProvider (as assumed, Contacts, provided by SDK) and SimpleCursorAdapter. The problem is that constructor of SimpleCursorAdapter containing the cursor from Contacts is deprecated. Documentations says:

This constructor is deprecated.

This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader.

My thoughts were: "Ok, i won't use it. I'll try LoaderManager with CursorLoader instead, as they are advicing me." So i went to the LoaderManager documentation site to find an example of use and what i found? Perfect example of using SimpleCursorAdapter constructor. Yes, the same i wanted to avoid becouse of it's deprecation.

    // Create an empty adapter we will use to display the loaded data.
    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);
    setListAdapter(mAdapter);

All the tutorials i could find are using this deprecated constructor. Can anyone provide me good answer what's the propper way to avoid using this? Or maybe i care about it too much? All i wanted was to learn good practices...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2024-12-14 12:31:31

如果您在 Android 2.2 上使用 LoaderManager,我假设您的项目中已经有了 Android 兼容性库。

在这种情况下,不要使用,

android.widget.SimpleCursorAdapter

因为该类只有一个现已弃用的构造函数。而是使用:

android.support.v4.widget.SimpleCursorAdapter

来自 compat 库。它有两个构造函数:

SimpleCursorAdapter(Context, int, Cursor, String[], int[]) // deprecated
SimpleCursorAdapter(Context, int, Cursor, String[], int[], int) // non-deprecated

问题中的代码示例使用第二个未弃用的构造函数,因此必须使用 SimpleCursorAdapter 的 compat lib 版本。

If you're using LoaderManager on Android 2.2 you already have the Android compatibility library in your project I assume.

In that case, don't use

android.widget.SimpleCursorAdapter

because that class only has a single, now deprecated constructor. Instead use:

android.support.v4.widget.SimpleCursorAdapter

from the compat library. It has two constructors:

SimpleCursorAdapter(Context, int, Cursor, String[], int[]) // deprecated
SimpleCursorAdapter(Context, int, Cursor, String[], int[], int) // non-deprecated

The code example in your question uses the second, non-deprecated constructor and thereby must be using the compat lib version of SimpleCursorAdapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文