问题描述
Android的SDK文档中说, startManagingCursor()
方法depracated:
Android SDK documentation says that startManagingCursor()
method is depracated:
此方法去precated。使用带有LoaderManager,而不是新的CursorLoader类;这也可通过Android兼容包旧的平台。这种方法允许活动采取基于活动的生命周期管理给定的游标的生命周期为您服务。即,当该活动停止它会自动调用停用()在给定的光标,而当它以后重新启动它会调用重新查询()为您服务。当活动被销毁,所有被管理的光标会自动关闭。如果你的目标蜂窝或更高版本,可以考虑使用,而不是代替LoaderManager,可通过getLoaderManager()
所以,我想用 CursorLoader
。但是,我怎么可以使用它与自定义的的CursorAdapter
和不的ContentProvider
,当我需要在<$ C $的构造URI C> CursorLoader ?
So I would like to use CursorLoader
. But how can I use it with custom CursorAdapter
and without ContentProvider
, when I needs URI in constructor of CursorLoader
?
推荐答案
我写了一个简单CursorLoader 不需要内容供应商:
I wrote a simple CursorLoader that does not need a content provider:
import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;
/**
* Used to write apps that run on platforms prior to Android 3.0. When running
* on Android 3.0 or above, this implementation is still used; it does not try
* to switch to the framework's implementation. See the framework SDK
* documentation for a class overview.
*
* This was based on the CursorLoader class
*/
public abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> {
private Cursor mCursor;
public SimpleCursorLoader(Context context) {
super(context);
}
/* Runs on a worker thread */
@Override
public abstract Cursor loadInBackground();
/* Runs on the UI thread */
@Override
public void deliverResult(Cursor cursor) {
if (isReset()) {
// An async query came in while the loader is stopped
if (cursor != null) {
cursor.close();
}
return;
}
Cursor oldCursor = mCursor;
mCursor = cursor;
if (isStarted()) {
super.deliverResult(cursor);
}
if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
oldCursor.close();
}
}
/**
* Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
* will be called on the UI thread. If a previous load has been completed and is still valid
* the result may be passed to the callbacks immediately.
* <p/>
* Must be called from the UI thread
*/
@Override
protected void onStartLoading() {
if (mCursor != null) {
deliverResult(mCursor);
}
if (takeContentChanged() || mCursor == null) {
forceLoad();
}
}
/**
* Must be called from the UI thread
*/
@Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
@Override
public void onCanceled(Cursor cursor) {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
@Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
if (mCursor != null && !mCursor.isClosed()) {
mCursor.close();
}
mCursor = null;
}
}
它只需要 AsyncTaskLoader
类。无论是一个在安卓3.0或更高版本,或附带的兼容包的人。
It only needs the AsyncTaskLoader
class. Either the one in Android 3.0 or higher, or the one that comes with the compatibility package.
我也wrote一个 ListLoader
这与 LoadManager
兼容,用于检索的通用的java.util.List
集合。
I also wrote a ListLoader
which is compatible with the LoadManager
and is used to retrieve a generic java.util.List
collection.
这篇关于没有ContentProvider的使用CursorLoader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!