问题描述
Android SDK文档说, startManagingCursor()
方法被折磨:
c $ c> CursorLoader 。但是当我需要在<$ c $的构造函数中使用URI时,如何使用它与自定义 CursorAdapter
和 ContentProvider
c> 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
类。
我也是,它与 LoadManager
兼容,用于检索通用的 java.util.List
集合。 / p>
Android SDK documentation says that startManagingCursor()
method is depracated:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!