Read data from a Custom Dialog generated in a Fragment

4

By clicking on the "Add Client" button that is seen in the first photo, it generates a dialog (of a layout) where I will enter data.

I'm doing the tests to capture that data with a Toast but it returns it as null: "Added clientenull" (I'm testing only with the Last Name field, I still do not validate).

Debugging I stumbled upon this error:

  

"SPAN_EXCLUSIVE_EXCLUSIVE spans can not have a zero length"

I found the possible solution that was adding this property to EditText: android: inputType="textNoSuggestions"

However, I'm still throwing the same error.

This is my portion of the code.

public class Clientes extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_clientes,container,false);
    Button btnAddClient = (Button)view.findViewById(R.id.btnAddClient);

    EditText txtDNI = (EditText)view.findViewById(R.id.txtDNI);
    EditText txtRUC = (EditText)view.findViewById(R.id.txtRUC);
    final EditText txtApellidos = (EditText)view.findViewById(R.id.txtApellidos);
    EditText txtNombres = (EditText)view.findViewById(R.id.txtNombres);
    EditText txtDireccion = (EditText)view.findViewById(R.id.txtDireccion);

    btnAddClient.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            LayoutInflater inflater = getActivity().getLayoutInflater();
            builder.setTitle("Agregar Cliente");
            builder.setView(R.layout.dialog_clientes);
            builder.setPositiveButton("Agregar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getActivity(), "Agregado cliente" + txtApellidos, Toast.LENGTH_SHORT).show();
                }
            });
            builder.show();
            builder.create();
        }
    });
    return view;
}
}
    
asked by Juan Sánchez 24.10.2016 в 18:43
source

1 answer

4

The message what you indicate is because you do not get the value correctly within EditText .

  

"Added clientenull"

To get the specified value within EditText , it is done using the getText () :

txtApellidos.getText().toString()

should be like this:

 Toast.makeText(getActivity(), "Agregado cliente: " + txtApellidos.getText().toString(), Toast.LENGTH_SHORT).show();

In your case, you mention that when obtaining the reference of EditText brand nullpointerException, I suggest you make a small change. Instead of directly using setView() , you get the view in which you would get the references of the elements by findViewById() :

...
...    
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setTitle("Agregar Cliente");

    //obtiene la vista en la cual se buscaran los elementos.
    View dialogView = inflater.inflate(R.layout.dialog_clientes, null);
    //builder.setView(R.layout.dialog_clientes);
    builder.setView(dialogView);

    final EditText txtApellidos = (EditText) dialogView.findViewById(R.id.txtApellidos);

    builder.setPositiveButton("Agregar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "Agregado cliente" + txtApellidos.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                });
...
...
    
answered by 24.10.2016 / 18:56
source