I can't grab the text from EditText object in DialogFragment ( getting an empty String ie. "" )

StackOverflow https://stackoverflow.com/questions/23418553

質問

I am extending DialogFragment and am having trouble retrieving the text that I enter through the DialogFragment UI. I am getting an empty string ("") for some reason.

Maybe: 1). It is not grabbing the correct view via View v = inflater.inflate(R.layout.add_dialog, null); 2). It is not grabbing the correct EditText 3). I need to use something through the DialogInterface object that is passed in via param

Thank you, Peter

Code.....

public class AddDialog extends DialogFragment {

EditText editText;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final foodhouseDatabaseAdapter myDBAdapter;
    myDBAdapter = new foodhouseDatabaseAdapter(this.getActivity());
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setMessage("Please enter the item to be added");
    builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // 'Close dialog box' ? .dismiss() ???
        }
    });

    builder.setNegativeButton("Add", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {


            View v = inflater.inflate(R.layout.add_dialog, null);

            /** 
             *  this line is setting 'editText' to an empty string
             */
            editText = (EditText) v.findViewById(R.id.user_entered_item_name);

            String x = editText.getText().toString();

            if (!x.isEmpty()) {

                // Capitalize first letter of user entered string
                String item = capitalizeFirstLetter(x);

                long ID = myDBAdapter.insertData(1, item);

                if (ID < 0) {
                    Message.message(getActivity(), "Item was not added");
                } else {
                    Message.message(getActivity(), item + " added");
                    editText.setText("");
                }
            } else {
                Message.message(getActivity(), "We did not see anything to insert");
            }

        }
    });
    builder.setView(inflater.inflate(R.layout.add_dialog, null));

    return builder.create(); 
}


/** http://stackoverflow.com/questions/5725892/
 *  how-to-capitalize-the-first-letter-of-word-in-a-string-using-java */
public String capitalizeFirstLetter(String original){
    if(original.length() == 0)
        return original;
    return original.substring(0, 1).toUpperCase(Locale.US) + original.substring(1);
}

}

役に立ちましたか?

解決

You are getting the inflater the wrong way. Use the getSystemService

example:

LayoutInflater inflater = (LayoutInflater) getActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

他のヒント

In your questions code remove View v = inflater.inflate(R.layout.add_dialog, null); editText = (EditText) v.findViewById(R.id.user_entered_item_name); from your onClick Listener and replace it with editText = (EditText) getDialog().findViewById(R.id.user_entered_item_name);.

The problem is you were inflating twice and not getting the first edit text you inflated and put in the Dialog with setView.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top