[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); }
ありがとうございます!それがonCreateView()と同じプロセスであることを知らなかった! –
私は自分のEditTextsにrootViewを使用すると、それらは常に空ですか? –
あなたの説明をより明確にするか、コードスニペットを共有してください。この目的のためにあなたの質問を編集することができます –