问题描述
我现在面临与装载机的问题。
I am facing an issue with Loader.
我有一个活动,它显示了从本地数据库检索的记录列表。当活动开始,记录会自动通过LoaderManager.initLoader()方法加载。
I have an Activity, which displays list of records retrieved from local DB. When the activity starts, records are automatically loaded via LoaderManager.initLoader() method.
也有可能以手动刷新经由ActionBarSherlock刷新按钮列表。然而,在完成其他活动还增加了一个记录到数据库后,onLoadFinished不叫。
There is also possibility to manually refresh the list via refresh button in ActionBarSherlock. However, after finishing another activity which adds a record to DB, onLoadFinished is not called.
我使用 SimpleCursorLoader 这里是code段从活动:
I am using SimpleCursorLoader and here is code snippet from the activity:
@Override
public void onStart() {
...
getSupportLoaderManager().initLoader(0, null, this);
}
@Override
public void onPause() {
...
getSupportLoaderManager().destroyLoader(0);
}
public void refreshRecords() {
getSupportLoaderManager().restartLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle args) {
Loader<Cursor> l = new SimpleCursorLoader(this) {
@Override
public Cursor loadInBackground() {
return recordDAO.getCursor();
}
};
l.forceLoad();
return l;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
// updateUI
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
的问题是,在完成其他活动后, onLoaderCreate
被调用,但 onLoaderFinished
不叫。
The issue is that after finishing the other activity, onLoaderCreate
is called, but onLoaderFinished
is not called.
一些调试后,我发现, SimpleCursorAdapter.deliverResults()
也叫,蕾结束了对 ..如果(isReset( )){..
after some debugging, I've found that SimpleCursorAdapter.deliverResults()
is also called, bud ends up on .. if (isReset()) { ..
我缺少的东西?如何强制数据的重载?
Am I missing something? How to force the reload of data?
感谢你在前进
推荐答案
我终于找到了解决这个问题的感谢讨论
I have finally found the solution to this problem thanks to the discussion on
https://groups.google.com/forum/#!topic/android-developers/DbKL6PVyhLI
public static <T> void initLoader(final int loaderId, final Bundle args, final LoaderCallbacks<T> callbacks,
final LoaderManager loaderManager) {
final Loader<T> loader = loaderManager.getLoader(loaderId);
if (loader != null && loader.isReset()) {
loaderManager.restartLoader(loaderId, args, callbacks);
} else {
loaderManager.initLoader(loaderId, args, callbacks);
}
}
这篇关于Android的 - onLoadFinished不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!