没有内容提供的CursorLoader使用AndroidSDK文档显示startManagingCursor()方法被实践:不推荐这种方法。与LoaderManager一起使用新的CursorLoader类;这也可以通过Android兼容性包在旧平台上使用。此方法允许活动根据活动的生命周期管理给定游标的生命周期。也就是说,当活动停止时,它将自动调用给定游标上的deactivate(),当它稍后重新启动时,它将为您调用requery()。当活动被销毁时,所有托管游标都将自动关闭。如果您的目标是蜂窝或以后,考虑使用LoaderManager代替,可以通过getLoaderManager()所以我想用CursorLoader..但是我如何使用它与自定义CursorAdapter没有ContentProvider的构造函数中需要URI时,CursorLoader?
3 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
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
ListLoader
LoadManager
java.util.List
函数式编程
TA贡献1807条经验 获得超9个赞
CursorLoader
犯罪嫌疑人X
TA贡献2080条经验 获得超4个赞
loadInBackground
:
public class CustomCursorLoader extends CursorLoader { private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver(); @Override public Cursor loadInBackground() { Cursor cursor = ... // get your cursor from wherever you like if (cursor != null) { // Ensure the cursor window is filled cursor.getCount(); cursor.registerContentObserver(mObserver); } return cursor; }};
- 3 回答
- 0 关注
- 544 浏览
添加回答
举报
0/150
提交
取消