2017-05-12 5 views
1

[OK]をクリックすると、返される文字列は常に空文字列""になります。そうではありませんが、ダイアログが2度作成されているので、同じものを指しているわけではありません。EditTexts[OK]をクリックすると、編集テキストが常に空になる

package com.example.gaetano.notebook; 


public class AddNoteFragment extends DialogFragment`enter code here`{ 

public EditText title; 
public EditText note; 
public OnNoteAdded noteAdded; 

public interface OnNoteAdded { 
    void onNoteAdded(Note note); 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    noteAdded = (OnNoteAdded) activity; 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    View rootView = inflater.inflate(R.layout.fragment_add_note, null); 

    title = (EditText) rootView.findViewById(R.id.edTitle); 
    note = (EditText) rootView.findViewById(R.id.edNote); 

    builder.setTitle("Add a new note"); 
    builder.setView(inflater.inflate(R.layout.fragment_add_note, null)) 
      .setPositiveButton(R.string.add_note, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        String t = title.getText().toString(); 
        String n = note.getText().toString(); 

        Note note = new Note(t, n , false); 
        noteAdded.onNoteAdded(note); 
       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        AddNoteFragment.this.getDialog().cancel(); 
       } 
      }); 
    return builder.create(); 
} 

[OK]をクリックすると、編集テキストは常に空です。

View rootView = inflater.inflate(R.layout.fragment_add_note, null); 

title = (EditText) rootView.findViewById(R.id.edTitle); 
note = (EditText) rootView.findViewById(R.id.edNote); 

私のsetPositiveButton, OnClickメソッドでは、これらのテキストの文字列を取得します。このString t = title.getText().toString(); String n = note.getText().toString(); を行うことで彼らはいつも私がこの@Override public void onNoteAdded(Note note) { noteAdapter.add(note); }

答えて

1

Layout Inflater APIによると、のInflaterのinflate()方法はViewオブジェクトを返すようなインターフェイスを実装しMainActivityで""

を返します。このViewオブジェクトのreferenceを変数に保持することができます。これはダイアログの画面にroot viewとして機能します。root viewを使用すると、このroot view's treeに含まれるすべての子ビューを見つけることができます。 findViewById()。 )ビューを作成して参照する処理がonCreateView(のものと同じである

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

LayoutInflater inflater = getActivity().getLayoutInflater(); 
View rootView=inflater.inflate(R.layout.fragment_add_note, null) 
Button mydialogbutton=(Button) rootView.findViewById(R.id.my_dialog_button); 
      //Button is exemplary but this is how you can access any child component of the root view 

builder.setTitle("Add a new note"); 
builder.setView(rootView) 
.setPositiveButton(R.string.add_note, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int id) { 
     Note newNote = new Note(
      "hey", 
      "notehey", 
      false); 
     noteAdded.onNoteAdded(newNote); 
    } 
}) 
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     AddNoteFragment.this.getDialog().cancel(); 
    } 
}); 
return builder.create(); 

- :ここ

onCreateDialog()ためのスニペットです。 onCreateDialog()onCreateView()の場合のように、レイアウトインフレータを使用してViewオブジェクトへの参照を取得しています。

+0

ありがとうございます!それがonCreateView()と同じプロセスであることを知らなかった! –

+0

私は自分のEditTextsにrootViewを使用すると、それらは常に空ですか? –

+1

あなたの説明をより明確にするか、コードスニペットを共有してください。この目的のためにあなたの質問を編集することができます –

関連する問題