studio programacion herramientas fundamentos con avanzado aplicaciones android dialog fragment communication

android - programacion - Comunicarse entre el fragmento y el diálogo de fragmento



manual de android en pdf (2)

Su fragmento establecerá oyente e implementará 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 tendrá una interfaz y un objeto de oyente:

// 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); } }

Investigué en SO sobre la comunicación entre el fragmento y el diálogo de fragmento, y encontré que setTargetFragment y una interfaz es un enfoque. Pero obtengo este error cuando intento establecerTargetFragment.

El método setTargetFragment (Fragment, int) en el tipo Fragment no es aplicable para los argumentos (SettingsDetailsFragment, int). No he usado fragmentos hasta ahora y soy nuevo en Android. Entonces tengo este fragmento:

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(); }

}


Verifica la importación de Fragment en tu clase SettingsDetailsFragment . Está importando android.support.v4.app.Fragment o android.app.Fragment .

La posibilidad es que esté configurando Fragment ofroid.support.v4.app como el destino de DialogFragment que es de android.app .