繁体   English   中英

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

[英]What's the propper way to use Cursor Adapters and Content Providers in android 2.2

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

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

不推荐使用此构造方法。

不建议使用此选项,因为它会导致在应用程序的UI线程上执行游标查询,从而可能导致响应能力差,甚至导致应用程序无响应错误。 或者,将LoaderManager与CursorLoader一起使用。

我的想法是:“好吧,我不会使用它。我会尝试使用带有CursorLoader的LoaderManager,因为它们会劝告我。” 因此,我去了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);

我能找到的所有教程都在使用此不建议使用的构造函数。 谁能给我一个很好的答案,避免使用此方法的正确方法是什么? 或者,也许我太在乎它了? 我只想学习良好的做法...

如果您在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版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM