My application saves blank data, I would like validation to prevent saving empty fields

Asked

Viewed 49 times

0

I would like my Edittext field validations to prevent saving null fields. I am beginner in the area and ask for everyone’s understanding. Thank you very much.

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import br.gestaoBd.BancoDeDados.ClienteDao;
 import br.gestaoBd.Beans.Cliente;

 public class CadCliente extends Activity {

EditText edId, edNome, edEmail, edTelefone, edEndereco, edCpf, edRg;
ClienteDao cliDao;

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.cad_clientes);
    edId = (EditText) findViewById(R.id.cadEdId);
    edNome = (EditText) findViewById(R.id.cadEdNome);
    edEmail = (EditText) findViewById(R.id.cadEdEmail);
    edTelefone = (EditText) findViewById(R.id.cadEdTelefone);
    edTelefone.addTextChangedListener(Mask.insert("(##)####-####", edTelefone));
    edEndereco = (EditText) findViewById(R.id.cadEdEndereco);
    edCpf = (EditText) findViewById(R.id.cadEdCpf);
    edCpf.addTextChangedListener(Mask.insert("###.###.###-##", edCpf));
    edRg = (EditText) findViewById(R.id.cadEdRg);
    edRg.addTextChangedListener(Mask.insert("##.###.###-#", edRg));

    Cliente clienteRecebido = (Cliente) getIntent().getSerializableExtra("Cliente");
    if (clienteRecebido != null) {
        montaTela(clienteRecebido);
    } else {
        montaTela(new Cliente());
    }

    Button btnSalvar = (Button) findViewById(R.id.btSalvar);
    btnSalvar.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Cliente cli = new Cliente();
            cli.setId(Integer.valueOf(edId.getText().toString()));
            cli.setNome(edNome.getText().toString());
            cli.setEmail(edEmail.getText().toString());
            cli.setTelefone(Mask.unmask(edTelefone.getText().toString()));
            cli.setEndereco(edEndereco.getText().toString());
            cli.setRg(Mask.unmask(edRg.getText().toString()));
            cli.setCpf(Mask.unmask(edCpf.getText().toString()));

            if (cli.getNome().equals("")) {
                edNome.setError("Preencha o nome");
            } else if (cli.getEmail().equals("")) {
                edEmail.setError("Preencha o Email");
            } else if (cli.getTelefone().equals("")) {
                edTelefone.setError("Preencha o telefone");
            } else if (cli.getEndereco().equals("")) {
                edEndereco.setError("Preencha o endereço");
            } else if (cli.getRg().equals("")) {
                edRg.setError("Preencha o RG");
            } else if (cli.getCpf().equals("")) {
                edCpf.setError("Preencha o CPF");
            }

            if (cli.getId() > 0) {
                getCliDao().alterar(cli);
                ToastManager.show(getBaseContext(), "Salvo com Sucesso",
                        ToastManager.INFORMATION);
            } else {
                getCliDao().inserirCliente(cli);
                ToastManager.show(getBaseContext(), "Salvo com Sucesso",
                        ToastManager.INFORMATION);
            }
        }

    }
    );

    Button btnLimpar = (Button) findViewById(R.id.btLimpar);

    btnLimpar.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            montaTela(new Cliente());
        }
    }
    );

    Button btExcluir = (Button) findViewById(R.id.btExcluir);
    btExcluir.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            final Cliente cli = new Cliente();
            cli.setId(Integer.valueOf(edId.getText().toString()));
            cli.setNome(edNome.getText().toString());
            cli.setEmail(edEmail.getText().toString());
            cli.setTelefone(edTelefone.getText().toString());
            cli.setEndereco(edEndereco.getText().toString());
            cli.setRg(edRg.getText().toString());
            cli.setCpf(edCpf.getText().toString());
            AlertDialog.Builder builder = new AlertDialog.Builder(CadCliente.this);
            builder.setTitle("Deseja Excluir?");
            builder.setMessage("O Cliente será deletado!");

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    getCliDao().excluir(cli);
                    montaTela(new Cliente());
                    ToastManager.show(getBaseContext(), "Cliente excluído",
                            ToastManager.INFORMATION);

                }

            });

            builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    ToastManager.show(getBaseContext(), "Cancelado",
                            ToastManager.INFORMATION);
                }
            });

            AlertDialog alert = builder.create();
            alert.show();

        }
    });

}

private void montaTela(Cliente cliente) {
    edId.setText(String.valueOf(cliente.getId()));
    edNome.setText(cliente.getNome());
    edEmail.setText(cliente.getEmail());
    edTelefone.setText(cliente.getTelefone());
    edEndereco.setText(cliente.getEndereco());
    edCpf.setText(cliente.getCpf());
    edRg.setText(cliente.getRg());

}

public ClienteDao getCliDao() {
    if (cliDao == null) {
        cliDao = new ClienteDao();
    }
    return cliDao;
}

}

1 answer

1


Places a Return inside the if for each validation:

if (cli.getNome().equals("")) {
    edNome.setError("Preencha o nome");
    return;

I don’t know if there is any framework to validate on androi, I don’t have much experience, but in your case a return; will make him stop processing the method and will not get to save.

Us else if also is to put. ok?

  • 2

    Perfect !! Thank you very much @Ricardo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.