Android での Async Task Loader と Fragment の使い方がよくわからないのかもしれません。私は開発者サイトの Google の例に従います (データベースでの削除を使用) が、どの場合でも Fragment クラスの onLoadFinished は常に古いデータを返します。フラグメントを再作成した場合にのみ、返されるデータは最新です。エラーはどこにありますか? ありがとう。
ここにコード。
断片的に
@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...]
}
AsyncTask ローダー内
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();
}