0

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

}

2 Answers2

1

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

example:

LayoutInflater inflater = (LayoutInflater) getActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Thx @Rod_Algonquin, but after getting the inflater how you suggested, the editText is still being set to an empty String (""). Do you think that using 'null' instead of a ViewGroup in this statement: View v = inflater.inflate(R.layout.add_dialog, null); may cause this ?? – Peter Baldwin May 02 '14 at 01:06
  • @PeterBaldwin instead of doing this builder.setView(inflater.inflate(R.layout.add_dialog, null)); just set the view like this builder.setView(v); so you can get your edditext – Rod_Algonquin May 02 '14 at 01:59
  • Thank you very much. I've spent far too much time on that issue. If it's not too much trouble, would you quickly tell me exactly why calling builder.setView() allowed me to grab that text and the other way would not? – Peter Baldwin May 02 '14 at 02:24
  • @PeterBaldwin everytime you inflate a view you need to set that view somewhere to enable to use that view you just inflater.. soo when you inflate you use it.. – Rod_Algonquin May 02 '14 at 02:25
0

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.

Simon
  • 10,932
  • 50
  • 49