CursorLoader的使用没有ContentProvider [英] CursorLoader usage without ContentProvider

查看:367
本文介绍了CursorLoader的使用没有ContentProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android SDK文档说, startManagingCursor()方法被折磨:


此方法已弃用。使用新的CursorLoader类与LoaderManager;这也可通过Android兼容包在较早的平台上提供。此方法允许活动根据活动的生命周期为您管理给定的Cursor的生命周期。也就是说,当活动停止时,它将自动调用给定Cursor上的deactivate(),当它重新启动时,它将为您调用requery()。当活动被销毁时,所有被管理的游标将自动关闭。如果你的目标是HONEYCOMB或更高版本,请考虑使用LoaderManager,而不是通过getLoaderManager()


c $ c> CursorLoader 。但是当我需要在<$ c $的构造函数中使用URI时,如何使用它与自定义 CursorAdapter ContentProvider c> CursorLoader ?

解决方案

我写了一个简单的CursorLoader 不需要内容提供者:

  import android.content.Context; 
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;

/ **
*用于编写在Android 3.0之前的平台上运行的应用程序。当在Android 3.0或更高版本上运行
*时,此实现仍然使用;它不尝试
*切换到框架的实现。有关类概述,请参阅框架SDK
*文档。
*
*这是基于CursorLoader类
* /
public abstract class SimpleCursorLoader extends AsyncTaskLoader< Cursor> {
private Cursor mCursor;

public SimpleCursorLoader(Context context){
super(context);
}

/ *在工作线程上运行* /
@Override
public abstract Cursor loadInBackground();

/ *在UI线程上运行* /
@Override
public void deliverResult(Cursor cursor){
if(isReset()){
//加载器停止时出现异步查询
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();
}
}

/ **
*开始异步加载联系人列表数据。当结果准备好时,回调
*将在UI线程上被调用。如果先前的加载已经完成并且仍然有效
*,则结果可以立即传递到回调。
*< p />
*必须从UI线程调用
* /
@Override
protected void onStartLoading(){
if(mCursor!= null){
deliverResult(mCursor);
}
if(takeContentChanged()|| mCursor == null){
forceLoad();
}
}

/ **
*必须从UI线程调用
* /
@Override
protected void onStopLoading(){
//如果可能,尝试取消当前加载任务。
cancelLoad();
}

@Override
public void onCanceled(Cursor cursor){
if(cursor!= null&!amp;!cursor.isClosed()){
cursor.close();
}
}

@Override
protected void onReset(){
super.onReset();

//确保加载器已停止
onStopLoading();

if(mCursor!= null&&!mCursor.isClosed()){
mCursor.close();
}
mCursor = null;
}
}

它只需要 AsyncTaskLoader 类。



我也是写了一个 ListLoader ,它与 LoadManager 兼容,用于检索通用的 java.util.List 集合。 / p>

Android SDK documentation says that startManagingCursor() method is depracated:

This method is deprecated. Use the new CursorLoader class with LoaderManager instead; this is also available on older platforms through the Android compatibility package. This method allows the activity to take care of managing the given Cursor's lifecycle for you based on the activity's lifecycle. That is, when the activity is stopped it will automatically call deactivate() on the given Cursor, and when it is later restarted it will call requery() for you. When the activity is destroyed, all managed Cursors will be closed automatically. If you are targeting HONEYCOMB or later, consider instead using LoaderManager instead, available via getLoaderManager()

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?

解决方案

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;
    }
}

It only needs the AsyncTaskLoader class. Either the one in Android 3.0 or higher, or the one that comes with the compatibility package.

I also wrote a ListLoader which is compatible with the LoadManager and is used to retrieve a generic java.util.List collection.

这篇关于CursorLoader的使用没有ContentProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆