If and Else problem when validating Edittext fields in the android app

Asked

Viewed 123 times

1

I’m a little confused to do a field validation here on my People Register. The following error appears: Else without if on line 84. I don’t have much experience and I appreciate the understanding of everyone who can help.

 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

2


The keys are wrong here:

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

The last line is on the outside, put it like this:

          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);
           }
  • It’s true. Thank you @Ricardo , however, is saving with empty fields.. Any suggestions ? I’m a little confused when doing field validation.

  • As for saving I do not know, I think it is better to close this question and open another, in the other you say how the table is, which fields are not saving, etc. It is much easier to meet. okay?

Browser other questions tagged

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