2017-09-12 5 views
0

親アクティビティからダイアログにデータを渡す際に助けが必要です。私はlongclickにテキストを設定しようとしています。私はホストからダイアログにテキストを渡す

 Dialog dialog = dialogFragment.getDialog(); 
    EditText editText = (EditText) dialog.findViewById(R.id.dialog_text); 
+0

一般的には、 'dialog'の 'arguments''Bundle'の中に' buttonText'を渡すことで、あなたの質問をあなたの 'JiffyDialogFragment'ソースで更新することができれば、私はあなたにそれを行う方法を正確に示すことができるかもしれません。 –

+1

https://stackoverflow.com/questions/15459209/passing-argument-to-dialogフラグメント - この解決策を確認してください。 –

+0

@BenP。 iveが私のダイアログフラグメントコードを追加しました – Donnovan

答えて

0

この行を呼び出すとき

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

ダイアログフラグメント・クラスのコード

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"); 

     } 
} 

マイアプリのクラッシュは、私は正しい方向に私を送るためにみんなありがとう問題を解決し

@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(); 
} 
関連する問題