35

我想知道如何在Android 3.0中使用Loaders,但似乎无法使其工作。该文档只描述使用CursorLoader,但我使用AsyncTaskLoaderAndroid Honeycomb中的装载机

从文档看来,你应该只需要实现AsyncTaskLoader.loadInBackground(),但它永远不会在getLoaderManager().initLoader()之后被调用,然后在回调中创建加载程序。

我可以看到调试消息说Created new loader LoaderInfo{4040a828 #0 : ArticleDataLoader{4036b350}}所以它看起来像它被成功创建。

装载机目前是否可能在SDK中被破坏,或者在创建装载机后需要调用某种方法? (他们在CursorLoader例子中没有这样做)。

编辑:好像呼吁从initLoader()返回的装载机forceLoad()至少开始加载,但是这意味着你不能正确处理旋转:(

+0

如果你找到答案,请让我知道。我一直无法找到任何东西。 – 2011-02-26 18:07:49

+2

还有http://code.google.com/p/android/issues/detail?id=14944提到与“编辑”评论相同的解决方法。 – 2011-02-26 19:55:59

+0

是的,这是我的bug报告:) – alexanderblom 2011-02-26 21:58:39

回答

0

亚历; 你尝试,如果onLoadInBackground验证()甚至调用?

onLoadInBackground():调用一个工作线程来执行实际的加载。实现不应该直接提供结果,但应该从这个方法返回它们,最终最终会调用deliverResult(D) UI线程如果实现需要在UI线程上处理结果,它们可以覆盖deliverRes超(D),并在那里做。

1

你需要重写onStartLoading()方法。看看developer website上的例子。

/** 
    * Handles a request to start the Loader. 
    */ 
    @Override protected void onStartLoading() { 
     if (mApps != null) { 
      // If we currently have a result available, deliver it 
      // immediately. 
      deliverResult(mApps); 
     } 

     // Start watching for changes in the app data. 
     if (mPackageObserver == null) { 
      mPackageObserver = new PackageIntentReceiver(this); 
     } 

     // Has something interesting in the configuration changed since we 
     // last built the app list? 
     boolean configChange = mLastConfig.applyNewConfig(getContext().getResources()); 

     if (takeContentChanged() || mApps == null || configChange) { 
      // If the data has changed since the last time it was loaded 
      // or is not currently available, start a load. 
      forceLoad(); 
     } 
    }