1

也许我不明白 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();


}
4

0 回答 0