Android的多接口实现

时间:2019-03-04 09:06:24

标签: java android interface callback this

我有一个Activity,它实现了两个接口,用于对某些操作进行回调。我想将Activity的上下文发送到另一个Class,该Class将访问指定回调的对象。

这是我的代码:-

     public class OpenSchoolFragment extends Fragment implements IOpenSchoolCallBack, INetworkCallback
    {
// 1st case
       expandableListAdapter = new AdapterOpenSchool(getActivity(), this); // IOpenSchoolCallBack should provided

//2nd case
call.enqueue(new Callback<OpenSchool>()
        {
            @Override
            public void onResponse(Call<OpenSchool> call, Response<OpenSchool> response)
            {

                if (response.isSuccessful())
                {
                    OpenSchool userBatch = response.body();
                    if (userBatch != null)
                    {
                        RLProgressRoot.setVisibility(View.GONE);
                    }
                }
                else
                {
                    RLProgressRoot.setVisibility(View.GONE);
                    if (response.code() == getResources().getInteger(R.integer.integer_404))
                    {
                        DialogHelperUtil.showRetrySnackbar(RLContainer, getString(R.string.str_error_unauthorised),this); //INetworkCallBack should be provided

                    }
                    else
                    {
                        DialogHelperUtil.showMessageSnackbar(RLContainer, response.raw().message());
                    }
                }
            }

        }

因此,当我尝试在第二种情况下传递“ this”时,出现一条错误消息:-

Wrong 3rd argument type. Found: 'Callback<OpenSchool>', required: 'INetworkCallback'

在第二种情况下,如何确保正确的回调在“ this”中传递。 INetworkCallback

1 个答案:

答案 0 :(得分:1)

“ this”是指您在该精确点处所在的当前对象。

如果您的DialogHelperUtil.showRetrySnackbar()是从Callback类中调用的,则this是指Callback

您可以使用Callback“退出”当前内部类(OuterClass.this)并达到其外部类