如何向Android AsnycTaskLoader传递参数

6

我想把参数传递给AsyncTaskLoader。我该怎么做?

目前,我将需要传递的内容放在一个单独的静态类中。有没有其他方法?

public class NewsAsyncTaskLoader extends AsyncTaskLoader<List<Content>> {

    private static final DbHelper dbHelper = DbHelperFactory.getDbHelper();

    public FeedAsyncTaskLoader(Context context) {
        super(context);
    }

    @Override
    public List<Content> loadInBackground() {
        List<Content> contents = DbHelper.getStream(FeedSections.getInstance().getCurrentSection());

        feed.setContents(contents);

        return feed;
    }
}
1个回答

18

将额外的参数传递到您的构造函数中:

public FeedAsyncTaskLoader(Context context, String moreInfo) {
    super(context);
    // Do something with moreInfo
}

1
我试过了。第一次可以工作。那么当您想使用不同的参数第二次刷新数据时会发生什么?onCreateLoader会再次被调用吗? - Bryan
2
使用LoaderManagerrestartLoader方法 - 每次都会调用您的onCreateLoader,并为您提供传递新参数的机会。 - ianhanniballake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接