sea*_*kej 107 android cursor android-contentprovider android-loadermanager android-cursorloader
Android SDK文档说该startManagingCursor()方法已被删除:
不推荐使用此方法.改为使用新的CursorLoader类和LoaderManager; 这也可以通过Android兼容包在旧版平台上使用.此方法允许活动根据活动的生命周期为您管理给定的Cursor生命周期.也就是说,当活动停止时,它将自动调用给定Cursor上的deactivate(),稍后重新启动它将为您调用requery().当活动被销毁时,所有托管游标将自动关闭.如果您的目标是HONEYCOMB或更高版本,请考虑改为使用LoaderManager,可通过getLoaderManager()获取
所以我想用CursorLoader.但是当我在构造函数中需要URI时,如何使用自定义CursorAdapter和不使用它?ContentProviderCursorLoader
Cri*_*ian 154
我写了一个不需要内容提供者的简单CursorLoader:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
它只需要AsyncTaskLoader上课.Android 3.0或更高版本中的一个,或兼容包附带的那个.
我还写了一个ListLoader与之兼容的LoadManager并且用于检索泛型java.util.List集合.
emm*_*mby 14
SimpleCursorLoader是一个简单的解决方案,但它不支持在数据更改时更新加载器.CommonsWare有一个loaderex库,可以添加SQLiteCursorLoader并支持对数据更改进行重新查询.
https://github.com/commonsguy/cwac-loaderex
Tim*_*Ohr 12
第三种选择是简单地覆盖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;
}
};
Run Code Online (Sandbox Code Playgroud)
这还将在数据库更改时重新查询光标.
唯一需要注意的是:你必须定义另一个观察者,因为Google的无限智慧决定让他们的包装变得私密.如果您将类放入与原始类(或compat)相同的包中,您实际上可以使用原始观察者.观察者是一个非常轻量级的对象,并没有在其他任何地方使用,所以这并没有太大的区别.
| 归档时间: |
|
| 查看次数: |
46505 次 |
| 最近记录: |