Android Loader返回错误的项目

时间:2013-09-30 16:31:21

标签: android

我正在尝试了解Loaders并拥有一个相当标准的设置 - 从数据库中提取的列表,当选择项目时,将检索并显示详细信息。它在第一次单击时运行良好,但始终返回相同的数据,而不管之后单击哪个列表项。我逐步完成了代码并确定一切正常,但实际数据被忽略,因为原始的加载器被重用了。这是一些代码:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    Cursor cursor  = (Cursor)this.getListAdapter().getItem(position);
    String mId = cursor.getString(cursor.getColumnIndex(COLUMN_ID));

    Bundle args = new Bundle();

    args.putString(ID, mId);
    LoaderManager.enableDebugLogging(true);
    LoaderManager lm = getLoaderManager();
    lm.initLoader(1, args, new NewsLoaderCallbacks());

}
// News item callbacks
private class NewsLoaderCallbacks implements LoaderCallbacks<SingleNewsItem> {
    @Override
    public Loader<SingleNewsItem> onCreateLoader(int id, Bundle args) {
        Log.i("DEBUG", "In oncreateloader");
        return new SingleNewsItemLoader(getActivity(), args.getString(ID));
    }

    @Override
    public void onLoadFinished(Loader<SingleNewsItem> itemLoader,
            SingleNewsItem item) {
        // display the item here
        Log.i("DEBUG", "In onloadfinished");
        Intent i = new Intent(getActivity(), SingleNewsItemActivity.class);
        //i.putExtra(ID, item.getId());
        i.putExtra("ID", item);
        startActivity(i);
    }

    @Override
    public void onLoaderReset(Loader<SingleNewsItem> arg0) {
        // nothing
    }

 public class SingleNewsItemLoader extends DataLoader<SingleNewsItem> {

String mNewsItemId;

public SingleNewsItemLoader(Context context, String id) {
    super(context);
    mNewsItemId = id;
}

@Override
public SingleNewsItem loadInBackground() {

    return NewsManager.get(getContext()).getOneItem(mNewsItemId);
}

 }

因此onCreateLoader在SingleNewsItemLoader中设置id属性,onLoadFinished接收详细项目。但似乎在列表中单击了不同的项目时,onCreate不会更新id属性。这是调试日志:

 09-30 16:04:25.997: V/LoaderManager(2599): initLoader in LoaderManager{40d16138 in  GoogleNewsMainActivity{40ced590}}: args=Bundle[{id=4 charged in mass shooting at park: 'God  took care of them' - Chicago Tribune}]
 09-30 16:04:25.997: V/LoaderManager(2599):   Starting: LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:04:26.006: V/LoaderManager(2599):   Created new loader LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:04:26.086: V/LoaderManager(2599): onLoadComplete: LoaderInfo{40e78278 #1 :   SingleNewsItemLoader{40e788f8}}
 09-30 16:04:26.097: V/LoaderManager(2599):   onLoadFinished in SingleNewsItemLoader{40e788f8 id=1}: SingleNewsItem{40e7bd88}
 09-30 16:04:33.376: V/LoaderManager(2599): Stopping in LoaderManager{40d16138 in GoogleNewsMainActivity{40ced590}}
 09-30 16:04:33.376: V/LoaderManager(2599):   Stopping: LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:04:33.396: V/LoaderManager(2599):   Stopping: LoaderInfo{40d16c78 #0 : NewsListCursorLoader{40d17aa8}}
 09-30 16:04:35.436: V/LoaderManager(2599): Starting in LoaderManager{40d16138 in GoogleNewsMainActivity{40ced590}}
 09-30 16:04:35.436: V/LoaderManager(2599):   Starting: LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:04:35.446: V/LoaderManager(2599): onLoadComplete: LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:04:35.446: V/LoaderManager(2599):   Starting: LoaderInfo{40d16c78 #0 : NewsListCursorLoader{40d17aa8}}
 09-30 16:04:35.446: V/LoaderManager(2599): onLoadComplete: LoaderInfo{40d16c78 #0 :  NewsListCursorLoader{40d17aa8}}
 09-30 16:04:39.136: V/LoaderManager(2599): initLoader in LoaderManager{40d16138 in GoogleNewsMainActivity{40ced590}}: args=Bundle[{id=Conservatives resolute on Obamacare, but Sen. Coburn says strategy won't work - Washington Post}]
 09-30 16:04:39.136: V/LoaderManager(2599):   Re-using existing loader LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:04:39.196: V/LoaderManager(2599):   onLoadFinished in SingleNewsItemLoader{40e788f8 id=1}: SingleNewsItem{40e7bd88}
 09-30 16:04:45.795: V/LoaderManager(2599): Stopping in LoaderManager{40d16138 in GoogleNewsMainActivity{40ced590}}
 09-30 16:04:45.795: V/LoaderManager(2599):   Stopping: LoaderInfo{40e78278 #1 : S   ingleNewsItemLoader{40e788f8}}
 09-30 16:04:45.805: V/LoaderManager(2599):   Stopping: LoaderInfo{40d16c78 #0 : NewsListCursorLoader{40d17aa8}}
 09-30 16:04:46.235: V/LoaderManager(2599): Starting in LoaderManager{40d16138 in GoogleNewsMainActivity{40ced590}}
 09-30 16:04:46.235: V/LoaderManager(2599):   Starting: LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:04:46.255: V/LoaderManager(2599): onLoadComplete: LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:04:46.255: V/LoaderManager(2599):   Starting: LoaderInfo{40d16c78 #0 : NewsListCursorLoader{40d17aa8}}
 09-30 16:04:46.296: V/LoaderManager(2599): onLoadComplete: LoaderInfo{40d16c78 #0 : NewsListCursorLoader{40d17aa8}}
 09-30 16:06:23.906: V/LoaderManager(2599): initLoader in LoaderManager{40d16138 in GoogleNewsMainActivity{40ced590}}: args=Bundle[{id=Obama directs Kerry to pursue talks with Iran over nuclear weapons deal - Fox News}]
 09-30 16:06:23.906: V/LoaderManager(2599):   Re-using existing loader LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:06:23.936: V/LoaderManager(2599):   onLoadFinished in SingleNewsItemLoader{40e788f8 id=1}: SingleNewsItem{40e7bd88}
 09-30 16:06:29.066: V/LoaderManager(2599): Stopping in LoaderManager{40d16138 in GoogleNewsMainActivity{40ced590}}
 09-30 16:06:29.066: V/LoaderManager(2599):   Stopping: LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:06:29.066: V/LoaderManager(2599):   Stopping: LoaderInfo{40d16c78 #0 : NewsListCursorLoader{40d17aa8}}
 09-30 16:06:34.316: V/LoaderManager(2599): Starting in LoaderManager{40d16138 in GoogleNewsMainActivity{40ced590}}
 09-30 16:06:34.316: V/LoaderManager(2599):   Starting: LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
 09-30 16:06:34.316: V/LoaderManager(2599): onLoadComplete: LoaderInfo{40e78278 #1 : SingleNewsItemLoader{40e788f8}}
  09-30 16:06:34.316: V/LoaderManager(2599):   Starting: LoaderInfo{40d16c78 #0 : NewsListCursorLoader{40d17aa8}}
 09-30 16:06:34.325: V/LoaderManager(2599): onLoadComplete: LoaderInfo{40d16c78 #0 : NewsListCursorLoader{40d17aa8}}

如果您将日志一直滚动到左侧,您可以看到ID已更改(它们是标题),但如果您在调试器中停止并查看返回的项目对象,则始终是第一个。所以SingleNewsItemLoader中的属性永远不会改变。

我在这里缺少什么?谢谢你的任何建议。

3 个答案:

答案 0 :(得分:1)

LoaderManager.initLoader()

  

确保加载程序已初始化并处于活动状态。如果加载器尚不存在,则创建一个加载器(如果活动/片段当前已启动),则启动加载器。 否则将重新使用上次创建的加载程序。

使用restartLoader以新参数重新启动加载程序。

答案 1 :(得分:0)

像ianhanniballake所说,你需要使用LoaderManager.restartLoader()LoaderManager.distroyLoader(),然后使用LoaderManager.initLoader()

答案 2 :(得分:0)

我认为您需要更改传递给initLoader()的ID,现在它是常量1,请尝试:

lm.initLoader(position, args, new NewsLoaderCallbacks());