问题
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.
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();
}
}
回答1:
Check the import for Fragment
in your SettingsDetailsFragment
class. Is it importing android.support.v4.app.Fragment
or android.app.Fragment
.
Possibility is that you are setting Fragment
of android.support.v4.app
as the target of DialogFragment which is of android.app
.
回答2:
Your fragment will set listener and implement EmailChangedListner :
public class SettingsDetailsFragment extends Fragment implements
EmailChangedListner {
public void emailUpdateFromDialog()
{
DialogFragment modifyEmailFragment = new ModifyEmailFragment();
modifyEmailFragment.setListener(SettingsDetailsFragment.this);
modifyEmailFragment.show(getActivity().getFragmentManager(),
"email");
}
}
Dialog Fragment will have interface and an object of listener :
// the dialogFragment
public class ModifyEmailFragment extends DialogFragment {
EmailChangedListner listener;
public void setListener(EmailChangedListner listener) {
this.listener = listener;
}
@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();
listener.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();
}
public interface EmailChangedListner {
public void emailChanged(String text);
}
}
来源:https://stackoverflow.com/questions/26483781/communicate-between-fragment-and-fragment-dialog