我有两个类(不是活动),一个接口带有带参数的单个方法。 在第一堂课中,我正在参数中发送数据。
print(2)
第二类(我刚刚实现了侦听器,还需要做其他事情吗?),我想确认调用了侦听器,以便可以获取数据:
private void sendProviderData(General provider) {
Singleton.getInstance().setProviderId(provider.getId());
Singleton.getInstance().setProviderIcon(provider.getIcon());
Singleton.getInstance().setProviderName(provider.getName());
//Listener
ResultListener resultListener;
resultListener = this;
resultListener.onActivityResult(CommonUtils.PICK_PROVIDER_CODE, CommonUtils.RESULT_OKAY);
dialog.dismiss();
}
答案 0 :(得分:1)
头等舱
private ResultListener resultListener;
----
---
public void setResultListener(ResultListener listener) {
this.resultListener = listener;
}
public void sendProviderData(General provider) {
Singleton.getInstance().setProviderId(provider.getId());
Singleton.getInstance().setProviderIcon(provider.getIcon());
Singleton.getInstance().setProviderName(provider.getName());
resultListener.onActivityResult(CommonUtils.PICK_PROVIDER_CODE, CommonUtils.RESULT_OKAY);
dialog.dismiss();
}
在第二类中,假设您具有FirstClass实例名称firstInstance
,需要将Second class设置为侦听器。在创建First类实例之后。这样您就可以得到回复
FirstClass firstInstance = new FirstClass();
firstInstance.setResultListener(this)
firstInstance.sendProviderData(provider);
.......
....
..
@Override
public void onActivityResult(int requestCode, int resultCode) {
showLog("???");
if (resultCode == RESULT_OKAY) {
switch (requestCode) {
case PICK_PROVIDER_CODE:
showLog("worked!");
/* providerId = data.getLongExtra(PROVIDER_ID, 0);
inputBillProvider.setText(data.getStringExtra(PROVIDER_NAME));
selected = data.getStringExtra(PROVIDER_NAME);
setProviderValidation(selected);*/
break;
}
}