在片段和片段对话框之间通信 [英] Communicate between fragment and fragment dialog
问题描述
我对SO进行了有关片段和片段对话框之间的通信的研究,发现setTargetFragment和接口是一种方法。但是,当我尝试设置setTargetFragment时出现此错误。
I researched on SO about communicating between fragment and fragment dialog, and I found that setTargetFragment and an interface is an approach. But I get this error when I try to setTargetFragment.
Fragment类型的setTargetFragment(Fragment,int)方法不适用于参数(SettingsDetailsFragment,int)。我还没有使用过片段,而我是android的新手。
所以我有这个片段:
The method setTargetFragment(Fragment, int) in the type Fragment is not applicable for the arguments (SettingsDetailsFragment, int). I haven;t use fragments until now and I'm new on android. So i have this fragment:
public class SettingsDetailsFragment extends Fragment implements
NoticeDialogFragment.EmailChangedListner {
public void emailUpdateFromDialog()
{
DialogFragment modifyEmailFragment = new ModifyEmailFragment();
//here is where i get the error
modifyEmailFragment.setTargetFragment(this, 0);`enter code here`
modifyEmailFragment.show(getActivity().getFragmentManager(),
"email");
}
}
// class where i have the interface
public class NoticeDialogFragment extends DialogFragment {
public interface EmailChangedListner {
public void emailChanged(String text);
}
}
// the dialogFragment
public class ModifyEmailFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
final View v_iew = inflater.inflate(R.layout.email_dialog, null);
builder.setView(v_iew)
// Add action buttons
.setPositiveButton(R.string.modifyBtn_settings,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText userEmail = (EditText) v_iew
.findViewById(R.id.modifyEmail);
boolean validateEmail = BookMeUtils
.validateEmail(userEmail.getText()
.toString());
if (validateEmail == false) {
BookMeUtils.enterValidEmail(getActivity());
}
if (!(userEmail.getText().toString().length() == 0)) {
if (validateEmail == true) {
Fragment parentFragment = getTargetFragment();
((EmailChangedListner) parentFragment)
.emailChanged(userEmail
.getText().toString());
}
} else {
}
}
})
.setNegativeButton(R.string.cancel_r,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ModifyEmailFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
推荐答案
在您的 SettingsDetailsFragment
类中检查 Fragment
的导入。是导入 android.support.v4.app.Fragment
还是 android.app.Fragment
。
Check the import for Fragment
in your SettingsDetailsFragment
class. Is it importing android.support.v4.app.Fragment
or android.app.Fragment
.
可能性是您正在设置 android.support.v4.app
片段 >作为DialogFragment的目标,该目标是 android.app
。
Possibility is that you are setting Fragment
of android.support.v4.app
as the target of DialogFragment which is of android.app
.
这篇关于在片段和片段对话框之间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!