I'm trying to validate some EditTexts inside an alert dialog, but when I click on the "OK" button the alert simply dismiss, as you can see in my code I have a error message that should appear if the field is empty, what am I missing here, any help would be appreciated.
final TextView id = (TextView) spinner.getSelectedView();
final EditText descricao = (EditText) v.findViewById(R.id.add_descricao);
final EditText observacao = (EditText) v.findViewById(R.id.add_observacao);
final EditText data = (EditText) v.findViewById(R.id.add_data);
final EditText valor = (EditText) v.findViewById(R.id.add_valor);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
data.setError(null);
valor.setError(null);
String campo_data = data.getText().toString();
String campo_valor = valor.getText().toString();
if (TextUtils.isEmpty(campo_data)) {
data.setError(getString(R.string.flObrigatorio));
focusView = data;
op = true;
}
if (TextUtils.isEmpty(campo_valor)) {
valor.setError(getString(R.string.flObrigatorio));
focusView = data;
op = true;
}
if (op) {
focusView.requestFocus();
} else {
DespesaModel mDespesaModel = new DespesaModel();
//Formata data para enviar para o banco
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Repositorio mRepositorio = new Repositorio(getApplicationContext());
DespesaCategoriaModel mDespesaCategoriaModel = mRepositorio.getCategoriaDespesaPorId(id.getId(), false);
mDespesaModel.setId_rm_empresa(Integer.valueOf(rm_IdEmpresa));
mDespesaModel.setId_rm_despesa_categoria(mDespesaCategoriaModel.getId_rm());
mDespesaModel.setId_despesa_categoria(mDespesaCategoriaModel.getId());
mDespesaModel.setId_rm_credencial(Integer.valueOf(mSessao.getString("id_credencial")));
mDespesaModel.setId_rm_viagem(Integer.valueOf(rm_IdViagem));
mDespesaModel.setData(mFerramentas.dataBr(campo_data));
mDespesaModel.setValor(valor.getText().toString().substring(2).replace(".", "").replace(",", "."));
mDespesaModel.setDescricao(descricao.getText().toString());
mDespesaModel.setObservacao(observacao.getText().toString());
mDespesaModel.setCriado(ft.format(dNow));
mDespesaModel.setModificado(ft.format(dNow));
mDespesaModel.setStatus("A");
mRepositorio.CadastrarDespesa(mDespesaModel, false);
carregaDespesas();
}
}
});
You're not telling the application that it should remain on AlertDialog
, you're only setting an error to an Object.
A solution is to add onShowListener
event into AlertDialog
where you can override the onClickListener
of the button.
Example:
final AlertDialog d = new AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
.setNegativeButton(android.R.string.cancel, null)
.create();
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Do something
//Dismiss once everything is OK.
d.dismiss();
}
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With