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