0

I need help passing data from my parent activity to a dialog. I am trying to set text on a longclick.

    public boolean onLongClick(View v) {
    RememberLongPressedButton(v);
    String buttonText = GetButtonText(v);
    DialogFragment newFragment = new SomeDialogFragment();
    newFragment.show(getFragmentManager(), _APPNAME);

    if(!(buttonText.equals(" ") || buttonText.isEmpty()))
        DataToEditText(newFragment,buttonText);

    return true;
}

The code for the Dialog fragment class

public class SomeDialogFragment extends DialogFragment {

public interface SomeDialogFragment {
    void onDialogPositiveClick(DialogFragment dialog);
    void onDialogNegativeClick(DialogFragment dialog);
}

private SomeDialogFragmentListener _listener;

@Override
public void onAttach(Context context) {
    super.onAttach(context);

        try {
            if(context instanceof SomeDialogFragmentListener)
                _listener = (SomeDialogFragmentListener) context;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(context.toString()
                    + " must implement SomeDialogFragmentListener");

        }
}

My app crashes when i call this line

        Dialog dialog = dialogFragment.getDialog();
    EditText editText = (EditText) dialog.findViewById(R.id.dialog_text);
Donnovan
  • 223
  • 1
  • 4
  • 12
  • Generally you would do this by passing `buttonText` inside the dialog's "arguments" `Bundle`. If you can update your question with your `JiffyDialogFragment` source, I might be able to show you exactly how to do it. – Ben P. Sep 12 '17 at 19:31
  • 1
    https://stackoverflow.com/questions/15459209/passing-argument-to-dialogfragment -- Check this solution. – VP4Android Sep 12 '17 at 19:31
  • @BenP. ive just added my dialogfragment code – Donnovan Sep 12 '17 at 20:00

1 Answers1

0

I solved the problem thanx guys for sending me in the right direction

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);

    _data = getArguments().getString("data");

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    View v = inflater.inflate(R.layout.dialog_text, null);
    EditText text = (EditText)v.findViewById(R.id.dialog_text);;
    text.setText(_data);

    builder.setView(v)
            .setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    _listener.onDialogPositiveClick(JiffyDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id) {
                    //JiffyDialogFragment.this.getDialog().cancel();
                    _listener.onDialogNegativeClick(JiffyDialogFragment.this);
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
}
Donnovan
  • 223
  • 1
  • 4
  • 12