背景:
我有一个CursorLoader
直接使用 SQLite 数据库而不是使用ContentProvider
. 此加载程序与ListFragment
由CursorAdapter
. 到现在为止还挺好。
为简化起见,我们假设 UI 上有一个删除按钮。当用户单击它时,我从数据库中删除一行,并调用onContentChanged()
我的加载器。此外,在onLoadFinished()
回调时,我调用notifyDatasetChanged()
我的适配器以刷新 UI。
问题:
当删除命令快速连续发生时,意味着快速连续onContentChanged()
调用,bindView()
最终将使用 stale data。这意味着一行已被删除,但 ListView 仍在尝试显示该行。这会导致光标异常。
我究竟做错了什么?
代码:
这是一个自定义 CursorLoader(基于Diane Hackborn 女士的建议)
/**
* An implementation of CursorLoader that works directly with SQLite database
* cursors, and does not require a ContentProvider.
*
*/
public class VideoSqliteCursorLoader extends CursorLoader {
/*
* This field is private in the parent class. Hence, redefining it here.
*/
ForceLoadContentObserver mObserver;
public VideoSqliteCursorLoader(Context context) {
super(context);
mObserver = new ForceLoadContentObserver();
}
public VideoSqliteCursorLoader(Context context, Uri uri,
String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
super(context, uri, projection, selection, selectionArgs, sortOrder);
mObserver = new ForceLoadContentObserver();
}
/*
* Main logic to load data in the background. Parent class uses a
* ContentProvider to do this. We use DbManager instead.
*
* (non-Javadoc)
*
* @see android.support.v4.content.CursorLoader#loadInBackground()
*/
@Override
public Cursor loadInBackground() {
Cursor cursor = AppGlobals.INSTANCE.getDbManager().getAllCameras();
if (cursor != null) {
// Ensure the cursor window is filled
int count = cursor.getCount();
registerObserver(cursor, mObserver);
}
return cursor;
}
/*
* This mirrors the registerContentObserver method from the parent class. We
* cannot use that method directly since it is not visible here.
*
* Hence we just copy over the implementation from the parent class and
* rename the method.
*/
void registerObserver(Cursor cursor, ContentObserver observer) {
cursor.registerContentObserver(mObserver);
}
}
我班级的一个片段ListFragment
,显示了LoaderManager
回调;以及refresh()
每当用户添加/删除记录时我调用的方法。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView = getListView();
/*
* Initialize the Loader
*/
mLoader = getLoaderManager().initLoader(LOADER_ID, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new VideoSqliteCursorLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
mAdapter.notifyDataSetChanged();
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
public void refresh() {
mLoader.onContentChanged();
}
MyCursorAdapter
只是一个普通的,newView()
被覆盖以返回新膨胀的行布局 XML 并bindView()
使用Cursor
将列绑定到View
行布局中的 s。
编辑 1
在深入研究了这一点之后,我认为这里的根本问题是CursorAdapter
处理底层Cursor
. 我试图了解它是如何工作的。
采取以下场景以更好地理解。
- 假设
CursorLoader
已经完成加载并且它返回Cursor
现在有 5 行的 a。 - 开始显示这些
Adapter
行。它移动Cursor
到下一个位置并调用getView()
- 此时,即使在呈现列表视图的过程中,也会从数据库中删除一行(例如,_id = 2)。
- 这就是问题所在-
CursorAdapter
已将 移动Cursor
到与已删除行相对应的位置。该bindView()
方法仍然尝试使用 this 访问该行的列Cursor
,这是无效的并且我们得到异常。
问题:
- 这种理解正确吗?我对上面的第 4 点特别感兴趣,我假设当一行被删除时,
Cursor
除非我要求,否则它不会被刷新。 - 假设这是正确的,我如何要求我
CursorAdapter
丢弃/中止它ListView
正在进行的渲染,并要求它使用新的Cursor
(通过Loader#onContentChanged()
and返回Adapter#notifyDatasetChanged()
)来代替?
PS向版主提出的问题:是否应该将此编辑移至单独的问题?
编辑 2
根据各种答案的建议,我对 s 如何工作的理解似乎存在根本性错误Loader
。事实证明:
Fragment
or根本Adapter
不应该直接操作。Loader
- 应该监视数据的
Loader
所有变化,并且只要数据变化就应该提供Adapter
新Cursor
的onLoadFinished()
。
有了这种理解,我尝试了以下更改。- 没有任何操作Loader
。refresh 方法现在什么都不做。
Loader
另外,为了调试和内部发生的事情ContentObserver
,我想出了这个:
public class VideoSqliteCursorLoader extends CursorLoader {
private static final String LOG_TAG = "CursorLoader";
//protected Cursor mCursor;
public final class CustomForceLoadContentObserver extends ContentObserver {
private final String LOG_TAG = "ContentObserver";
public CustomForceLoadContentObserver() {
super(new Handler());
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
@Override
public void onChange(boolean selfChange) {
Utils.logDebug(LOG_TAG, "onChange called; selfChange = "+selfChange);
onContentChanged();
}
}
/*
* This field is private in the parent class. Hence, redefining it here.
*/
CustomForceLoadContentObserver mObserver;
public VideoSqliteCursorLoader(Context context) {
super(context);
mObserver = new CustomForceLoadContentObserver();
}
/*
* Main logic to load data in the background. Parent class uses a
* ContentProvider to do this. We use DbManager instead.
*
* (non-Javadoc)
*
* @see android.support.v4.content.CursorLoader#loadInBackground()
*/
@Override
public Cursor loadInBackground() {
Utils.logDebug(LOG_TAG, "loadInBackground called");
Cursor cursor = AppGlobals.INSTANCE.getDbManager().getAllCameras();
//mCursor = AppGlobals.INSTANCE.getDbManager().getAllCameras();
if (cursor != null) {
// Ensure the cursor window is filled
int count = cursor.getCount();
Utils.logDebug(LOG_TAG, "Count = " + count);
registerObserver(cursor, mObserver);
}
return cursor;
}
/*
* This mirrors the registerContentObserver method from the parent class. We
* cannot use that method directly since it is not visible here.
*
* Hence we just copy over the implementation from the parent class and
* rename the method.
*/
void registerObserver(Cursor cursor, ContentObserver observer) {
cursor.registerContentObserver(mObserver);
}
/*
* A bunch of methods being overridden just for debugging purpose.
* We simply include a logging statement and call through to super implementation
*
*/
@Override
public void forceLoad() {
Utils.logDebug(LOG_TAG, "forceLoad called");
super.forceLoad();
}
@Override
protected void onForceLoad() {
Utils.logDebug(LOG_TAG, "onForceLoad called");
super.onForceLoad();
}
@Override
public void onContentChanged() {
Utils.logDebug(LOG_TAG, "onContentChanged called");
super.onContentChanged();
}
}
这是我的片段Fragment
和LoaderCallback
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView = getListView();
/*
* Initialize the Loader
*/
getLoaderManager().initLoader(LOADER_ID, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new VideoSqliteCursorLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Utils.logDebug(LOG_TAG, "onLoadFinished()");
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
public void refresh() {
Utils.logDebug(LOG_TAG, "CamerasListFragment.refresh() called");
//mLoader.onContentChanged();
}
现在,每当数据库发生变化(添加/删除行)时,应该调用的onChange()
方法 - 对吗?ContentObserver
我没有看到这种情况发生。我的ListView
从未表现出任何变化。我看到任何变化的唯一一次是如果我明确地调用onContentChanged()
.Loader
这里出了什么问题?
编辑 3
好的,所以我重写了我Loader
的直接从AsyncTaskLoader
. 我仍然没有看到我的数据库更改被刷新,也没有看到我在数据库中插入/删除一行时被调用的onContentChanged()
方法:-(Loader
只是为了澄清一些事情:
我使用了代码
CursorLoader
并只修改了一行返回Cursor
.ContentProvider
在这里,我用我的代码替换了对的调用DbManager
(它反过来用于DatabaseHelper
执行查询并返回Cursor
)。Cursor cursor = AppGlobals.INSTANCE.getDbManager().getAllCameras();
我对数据库的插入/更新/删除是从其他地方发生的,而不是通过
Loader
. 在大多数情况下,数据库操作发生在后台Service
,在少数情况下,从Activity
. 我直接使用我的DbManager
类来执行这些操作。
我仍然没有得到的是 -谁告诉我Loader
一行已被添加/删除/修改?换句话说,在哪里被ForceLoadContentObserver#onChange()
调用?在我的加载器中,我在以下位置注册了我的观察者Cursor
:
void registerContentObserver(Cursor cursor, ContentObserver observer) {
cursor.registerContentObserver(mObserver);
}
这意味着当它发生变化时有责任Cursor
通知它。mObserver
但是,然后 AFAIK,“光标”不是“实时”对象,它会在数据库中修改数据时更新它指向的数据。
这是我的 Loader 的最新版本:
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;
public class VideoSqliteCursorLoader extends AsyncTaskLoader<Cursor> {
private static final String LOG_TAG = "CursorLoader";
final ForceLoadContentObserver mObserver;
Cursor mCursor;
/* Runs on a worker thread */
@Override
public Cursor loadInBackground() {
Utils.logDebug(LOG_TAG , "loadInBackground()");
Cursor cursor = AppGlobals.INSTANCE.getDbManager().getAllCameras();
if (cursor != null) {
// Ensure the cursor window is filled
int count = cursor.getCount();
Utils.logDebug(LOG_TAG , "Cursor count = "+count);
registerContentObserver(cursor, mObserver);
}
return cursor;
}
void registerContentObserver(Cursor cursor, ContentObserver observer) {
cursor.registerContentObserver(mObserver);
}
/* Runs on the UI thread */
@Override
public void deliverResult(Cursor cursor) {
Utils.logDebug(LOG_TAG, "deliverResult()");
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();
}
}
/**
* Creates an empty CursorLoader.
*/
public VideoSqliteCursorLoader(Context context) {
super(context);
mObserver = new ForceLoadContentObserver();
}
@Override
protected void onStartLoading() {
Utils.logDebug(LOG_TAG, "onStartLoading()");
if (mCursor != null) {
deliverResult(mCursor);
}
if (takeContentChanged() || mCursor == null) {
forceLoad();
}
}
/**
* Must be called from the UI thread
*/
@Override
protected void onStopLoading() {
Utils.logDebug(LOG_TAG, "onStopLoading()");
// Attempt to cancel the current load task if possible.
cancelLoad();
}
@Override
public void onCanceled(Cursor cursor) {
Utils.logDebug(LOG_TAG, "onCanceled()");
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
@Override
protected void onReset() {
Utils.logDebug(LOG_TAG, "onReset()");
super.onReset();
// Ensure the loader is stopped
onStopLoading();
if (mCursor != null && !mCursor.isClosed()) {
mCursor.close();
}
mCursor = null;
}
@Override
public void onContentChanged() {
Utils.logDebug(LOG_TAG, "onContentChanged()");
super.onContentChanged();
}
}