我有一个 Activity,它实现了两个接口(interface),用于回调某些操作。我想将 Activity 的上下文发送到另一个类,该类将访问指定回调的对象。
这是我的代码:-
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
?
最佳答案
“this”指的是您所在的当前对象。
如果您的 DialogHelperUtil.showRetrySnackbar()
是从 Callback
类调用的,则 this
引用 Callback
。
您可以使用OuterClass.this
“退出”当前内部类(Callback
)并到达其外部类
关于java - Android 多种接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54979861/