同一活动中的多个加载器 [英] Multiple loaders in same activity

查看:91
本文介绍了同一活动中的多个加载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个自定义构建的加载器继承自 AsyncTaskLoader ,我想在我的活动中使用它。每个都返回不同类型的结果。
要将我的活动用于回调,我必须实现两个接口:

I have two custom built loaders inherited from AsyncTaskLoader which I would like to use in my activity. Each of them returns result of different type. To use my activity for a callback I must implement two interfaces:

implements LoaderCallbacks<GetSyncListDataResult>, LoaderCallbacks<ErrorResult>

但是,尝试在同一个类中实现所需的方法我最终使用
重复方法错误和擦除(???)错误:

However, trying to implement required methods in the same class I end up with duplicate method error and erasure(???) error:

// Methods for the first loader
public Loader<GetSyncListDataResult> onCreateLoader(int ID, Bundle bundle) ...
public void onLoaderReset(Loader<GetSyncListDataResult> loader) ...
public void onLoadFinished(Loader<GetSyncListDataResult> loader, GetSyncListDataResult result) ...

// Methods for the second loader
public Loader<ErrorResult> onCreateLoader(int ID, Bundle bundle) ...
public void onLoaderReset(Loader<ErrorResult> loader) ...
public void onLoadFinished(Loader<ErrorResult> loader, ErrorResult result) ...

显然,方法是冲突的,我需要一个简单的方法来解决这个问题。
解决这个问题的正确方法是什么?

Obviously, the methods are clashing and I need an easy way how to resolve this. What would be the proper way of resolving this?

推荐答案

正确答案是根据@ dymmeh的评论,即不是为活动实现两个 LoaderCallbacks 接口,而是为了包含两个 LoaderCallbacks 实现。举例来说:在您的活动中初始化您的 LoaderCallbacks 字段...

The correct answer is as per @dymmeh's comment, i.e. not for the Activity to implement two LoaderCallbacks interfaces but for the activity to contain two LoaderCallbacks implementations. By way of example: initialise your LoaderCallbacks fields in your activity...

private LoaderCallbacks<GetSyncListDataResult> dataResultLoaderListener
  = new LoaderCallbacks<GetSyncListDataResult>() { ...methods here... };

private LoaderCallbacks<ErrorResult> errorResultLoaderListener
  = new LoaderCallbacks<ErrorResult>() { ...methods here... };

...并声明你的装载机ID ...

... and declare your loader ids...

private static final int DATA_RESULT_LOADER_ID = 1;
private static final int ERROR_RESULT_LOADER_ID = 2;

...然后初始化您的装载机......

... and then initialise your loaders...

getLoaderManager().initLoader(DATA_RESULT_LOADER_ID, dataResultBundle, dataResultLoaderListener);
getLoaderManager().initLoader(ERROR_RESULT_LOADER_ID, errorResultBundle, errorResultLoaderListener);

...完成!

这篇关于同一活动中的多个加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆