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.
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, Contact开发者_高级运维s, 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...
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
.
精彩评论