Android AsyncTask加载程序、返回的片段和数据
也许我不理解Android中异步任务加载器和片段的用法。我在开发者网站上遵循Google的示例(使用数据库上的deleting),但在任何情况下,片段类中的onLoadFinished都会返回旧数据。只有重新创建片段,返回的数据才是最新的。错误在哪里? 谢谢 这是代码 在片段中Android AsyncTask加载程序、返回的片段和数据,android,fragment,android-loadermanager,asynctaskloader,Android,Fragment,Android Loadermanager,Asynctaskloader,也许我不理解Android中异步任务加载器和片段的用法。我在开发者网站上遵循Google的示例(使用数据库上的deleting),但在任何情况下,片段类中的onLoadFinished都会返回旧数据。只有重新创建片段,返回的数据才是最新的。错误在哪里? 谢谢 这是代码 在片段中 @Override public Loader<List<Poi>> onCreateLoader(int id, Bundle args) {
@Override
public Loader<List<Poi>> onCreateLoader(int id, Bundle args) {
return new FavPoiLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<List<Poi>> loader, List<Poi> pois) {
addFavsOnSlidingMenu(pois);
}
@Override
public void onLoaderReset(Loader<List<Poi>> loader) {
// TODO Auto-generated method stub
}
private void addFavsOnSlidingMenu(List<Poi> listPois) {
pois = new ArrayList<Poi>();
pois = listPois;
[other...]
}
我添加了代码。谢谢各位:)当数据发生更改时,您需要调用
restartLoader(…)
。
public class FavPoiLoader extends AsyncTaskLoader<List<Poi>> {
private List<Poi> pois;
public FavPoiLoader(Context context) {
super(context);
}
@Override
public List<Poi> loadInBackground() {
final Context context = getContext();
List<Poi> pois = new ArrayList<Poi>();
PoiFavDaoImpl poiFavDao = new PoiFavDaoImpl(context);
PoiDaoImpl poiDao = new PoiDaoImpl(context);
List<Integer> idFavPois = new ArrayList<Integer>();
idFavPois = poiFavDao.listFavourite();
for(int favPoi: idFavPois) {
Poi poi = new Poi();
poi = poiDao.findPoiById(favPoi);
pois.add(poi);
}
return pois;
}
@Override
public void deliverResult(List<Poi> poisList) {
if (isReset()) {
// The Loader has been reset; ignore the result and invalidate the data.
if (poisList != null) {
releaseResources(poisList);
}
return;
}
// Hold a reference to the old data so it doesn't get garbage collected.
// We must protect it until the new data has been delivered.
List<Poi> oldData = pois;
pois = poisList;
if (isStarted()) {
// If the Loader is in a started state, deliver the results to the
// client. The superclass method does this for us.
super.deliverResult(poisList);
}
// Invalidate the old data as we don't need it any more.
if (oldData != null) {
releaseResources(oldData);
}
}
@Override
protected void onStartLoading() {
if (pois != null) {
// Deliver any previously loaded data immediately.
deliverResult(pois);
}
if (takeContentChanged() || pois == null) {
forceLoad();
}
}
@Override
protected void onStopLoading() {
// The Loader is in a stopped state, so we should attempt to cancel the
// current load (if there is one).
cancelLoad();
}
@Override
protected void onReset() {
super.onReset();
// Ensure the loader has been stopped.
onStopLoading();
// At this point we can release the resources associated with 'mData'.
if (pois != null) {
releaseResources(pois);
pois = null;
}
}
@Override
public void onCanceled(List<Poi> poisList) {
// Attempt to cancel the current asynchronous load.
super.onCanceled(poisList);
// The load has been canceled, so we should release the resources
// associated with 'data'.
releaseResources(poisList);
}
private void releaseResources(List<Poi> poisList) {
// For a simple List, there is nothing to do. For something like a Cursor, we
// would close it in this method. All resources associated with the Loader
// should be released here.
}
}
public void onClickImageButton(View v) {
int idPoi = (Integer) v.getTag();
listFavPoiAdapter.removeFav(idPoi);
poiFavDao = new PoiFavDaoImpl(this);
poiFavDao.delete(idPoi);
favPoiLoader.onContentChanged();
}