我正在使用包含2个片段的步进器表单,我需要将每个片段的值数据传递给activity并通过API发送值。问题是,我从片段收到的值只有1,而另一个是null。
此代码显示了片段A如何传递数据
@Override
public boolean onNextButtonHandler() {
String personalName = txtNamaRegist.getText().toString();
if(!personalName.equals("")){
mOnFragmentPassing.sendPersonalName(personalName);
}
return true;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mOnFragmentPassing = (OnFragmentPassing) getActivity();
}catch(ClassCastException e){
Log.e(TAG, "onAttach: ClassCastException : " + e.getMessage() );
}
}
此代码显示了片段B如何传递数据
@Override
public boolean onNextButtonHandler() {
String dataNamaToko = txtNamaToko.getText().toString();
if(!dataNamaToko.equals("")){
mOnFragmentPassing.sendNamaToko(dataNamaToko);
}
return true;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mOnFragmentPassing = (OnFragmentPassing) getActivity();
}catch(ClassCastException e){
Log.e(TAG, "onAttach: ClassCastException : " + e.getMessage() );
}
}
这是我的界面
public interface OnFragmentPassing {
void sendPersonalName(String personalName);
void sendNamaToko(String dataNamaToko);
}
这是我从片段
接收数据的活动代码@Override
public void sendPersonalName(String personalName) {
mPersonalName = personalName;
}
@Override
public void sendNamaToko(String dataNamaToko) {
mNamaToko = dataNamaToko;
}
@Override
public void onStepperCompleted() {
showCompletedDialog();
}
protected void showCompletedDialog(){
Toast.makeText(this, "nama "+ mPersonalName + mNamaToko, Toast.LENGTH_SHORT).show();
}