简体   繁体   English

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

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

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. 我尝试遵循Virgil Dobjanschi在Google IO 2010上的演讲“ 开发Android REST客户端应用程序 ”中给出的说明。不幸的是,我找不到实现Content Provider和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. 例如,让我们尝试使用Contacts ContentProvider而不是我自己的。 I tried the simplest solution - any ContentProvider (as assumed, Contacts, provided by SDK) and SimpleCursorAdapter. 我尝试了最简单的解决方案-任何ContentProvider(假定为SDK提供的Contacts)和SimpleCursorAdapter。 The problem is that constructor of SimpleCursorAdapter containing the cursor from Contacts is deprecated. 问题是不建议使用包含Contacts中的光标的SimpleCursorAdapter的构造函数。 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. 不建议使用此选项,因为它会导致在应用程序的UI线程上执行游标查询,从而可能导致响应能力差,甚至导致应用程序无响应错误。 As an alternative, use LoaderManager with a CursorLoader. 或者,将LoaderManager与CursorLoader一起使用。

My thoughts were: "Ok, i won't use it. I'll try LoaderManager with CursorLoader instead, as they are advicing me." 我的想法是:“好吧,我不会使用它。我会尝试使用带有CursorLoader的LoaderManager,因为它们会劝告我。” So i went to the LoaderManager documentation site to find an example of use and what i found? 因此,我去了LoaderManager文档站点以查找使用示例,我发现了什么? Perfect example of using SimpleCursorAdapter constructor. 使用SimpleCursorAdapter构造函数的完美示例。 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... 我只想学习良好的做法...

If you're using LoaderManager on Android 2.2 you already have the Android compatibility library in your project I assume. 如果您在Android 2.2上使用LoaderManager ,那么我认为您的项目中已经具有Android兼容性库。

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. 来自compat库。 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 . 您问题中的代码示例使用第二个不建议使用的构造函数,因此必须使用SimpleCursorAdapter的compat lib版本。

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

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