I have a problem.

Data from EditText empty, and with Spinner I get the first value. I tried different combinations.

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setView(R.layout.add_recipe) .setPositiveButton("Додати", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { AppCompatSpinner spinner; EditText title; EditText description; View view; view = LayoutInflater.from(getActivity()).inflate(R.layout.add_recipe, null); spinner = (AppCompatSpinner) view.findViewById(R.id.spinner); title = (EditText) view.findViewById(R.id.et_title); description = (EditText) view.findViewById(R.id.et_description); Recipe recipe = new Recipe(); String ms = title.getText().toString().trim(); Log.i("message", "you add" + ms); recipe.setName(title.getText().toString()); recipe.setDescription(description.getText().toString()); recipe.setTable(Tables.values()[spinner.getSelectedItemPosition()].getTableName()); RecipeLab.getInstance(getActivity()).addRecipe(recipe); dialog.cancel(); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); return builder.create(); 

Who met with such a problem help.

    1 answer 1

    You in the listener are loading a new instance of the dialog markup instead of referring to the markup of the displayed dialog. Try building a method argument to the dialog and take its markup like this:

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setView(R.layout.add_recipe) .setPositiveButton("Додати", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { AppCompatSpinner spinner; EditText title; EditText description; Dialog view; view = ((Dialog)dialog); ... } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); return builder.create(); 
    • java.lang.ClassCastException does not work. By the way, all this is in DialogFragnent - Developer
    • In the error you have written what class got me. Try it. Perhaps this is AlertDialog - YuriiSPb
    • one
      it works, thanks for the help. Exception is my fault instead of Dialog view, I wrote View view - Developer