0

¿Como valido un Edittext vacío en android? cuando está vacío debe ser igual a 0 y cuando el usuario le inserta un número lo envía al servidor, en mi php tengo que si no recibe el dato lo manda como 0 con un isset, mi código es el siguiente, lo que hace es que si el campo esta vacío manda un error en un toast y si no lo convierte en entero y lo manda al servidor, lo que quiero es que en vez de enviar el error lo iguale a un 0 y si no esta vacío lo manda al servidor, lo segundo ya lo hace solo quiero saber como igualarlo a 0. por favor

    if (etpri.getText().toString().isEmpty() || etpan.getText().toString().isEmpty() || etprd.getText().toString().isEmpty() ||
            etpt.getText().toString().isEmpty() || etpv.getText().toString().isEmpty() || etmc.getText().toString().isEmpty() ||
            etpanal.getText().toString().isEmpty() || etmorena.getText().toString().isEmpty() || etpes.getText().toString().isEmpty() ||
            etpri_pv.getText().toString().isEmpty() || etpan_prd_mc.getText().toString().isEmpty() || etpan_prd.getText().toString().isEmpty() ||
            etpan_mc.getText().toString().isEmpty() || etprd_mc.getText().toString().isEmpty() || etmorena_pt_pes.getText().toString().isEmpty() ||
            etmorena_pt.getText().toString().isEmpty() || etmorena_pes.getText().toString().isEmpty() || etpt_pes.getText().toString().isEmpty() ||
            etc1.getText().toString().isEmpty() || etnulos.getText().toString().isEmpty() || etnoregistrados.getText().toString().isEmpty() ||
            ettotal.getText().toString().isEmpty())
    {



    }else{
            final int pri = Integer.parseInt(etpri.getText().toString());
            final int pan = Integer.parseInt(etpan.getText().toString());
            final int prd = Integer.parseInt(etprd.getText().toString());
            final int pt = Integer.parseInt(etpt.getText().toString());
            final int pv = Integer.parseInt(etpv.getText().toString());
            final int mc = Integer.parseInt(etmc.getText().toString());
            final int panal = Integer.parseInt(etpanal.getText().toString());
            final int morena = Integer.parseInt(etmorena.getText().toString());
            final int pes = Integer.parseInt(etpes.getText().toString());
            final int pri_pv = Integer.parseInt(etpri_pv.getText().toString());
            final int pan_prd_mc = Integer.parseInt(etpan_prd_mc.getText().toString());
            final int pan_prd = Integer.parseInt(etpan_prd.getText().toString());
            final int pan_mc = Integer.parseInt(etpan_mc.getText().toString());
            final int prd_mc = Integer.parseInt(etprd_mc.getText().toString());
            final int morena_pt_pes = Integer.parseInt(etmorena_pt_pes.getText().toString());
            final int morena_pt = Integer.parseInt(etmorena_pt.getText().toString());
            final int morena_pes = Integer.parseInt(etmorena_pes.getText().toString());
            final int pt_pes = Integer.parseInt(etpt_pes.getText().toString());
            final int c1 = Integer.parseInt(etc1.getText().toString());
            final int nulos = Integer.parseInt(etnulos.getText().toString());
            final int noregistrados = Integer.parseInt(etnoregistrados.getText().toString());
            final int total = Integer.parseInt(ettotal.getText().toString());

2 respuestas 2

0

lo hice de la siguiente manera, pero no se ve nada reflejado en el phpmyadmin.

@Override public void onClick(View view) {

    AlertDialog.Builder a_builder = new AlertDialog.Builder(RegistroFragment.this);
    a_builder.setMessage("Desea hacer el registro de votos !!!")
            .setCancelable(false)
            .setPositiveButton("SI",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(RegistroFragment.this, FotoActivity.class);
                    intent.putExtra("casillaseleccionada", casillaseleccionada);
                    RegistroFragment.this.startActivity(intent);
                }
            })
            .setNegativeButton("NO",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            }) ;
    AlertDialog alert = a_builder.create();
    alert.setTitle("Alert !!!");
    alert.show();

    //Toast.makeText(RegistroFragment.this, "Error al subir los datos, campos vacíos", Toast.LENGTH_SHORT).show();

    String auxpri=etpri.getText().toString();
    String auxpan=etpan.getText().toString();
    String auxprd=etprd.getText().toString();
    String auxpt=etpt.getText().toString();
    String auxpv=etpv.getText().toString();
    String auxmc=etmc.getText().toString();
    String auxpanal=etpanal.getText().toString();
    String auxmorena=etmorena.getText().toString();
    String auxpes=etpes.getText().toString();
    String auxpripv=etpri_pv.getText().toString();
    String auxpanprdmc=etpan_prd_mc.getText().toString();
    String auxpanprd=etpan_prd.getText().toString();
    String auxpanmc=etpan_mc.getText().toString();
    String auxprdmc=etprd_mc.getText().toString();
    String auxmpp=etmorena_pt_pes.getText().toString();
    String auxmp=etmorena_pt.getText().toString();
    String auxmpes=etmorena_pes.getText().toString();
    String auxpp=etpt_pes.getText().toString();
    String auxc1=etc1.getText().toString();
    String auxnulos=etnulos.getText().toString();
    String auxnr=etnoregistrados.getText().toString();
    String auxt=ettotal.getText().toString();


    if (auxpri.matches("") || auxpan.matches("") || auxprd.matches("") || auxpt.matches("") || auxpv.matches("") || auxmc.matches("") || auxpanal.matches("") || auxmorena.matches("")
            || auxpes.matches("") || auxpripv.matches("") || auxpanprdmc.matches("") || auxpanprd.matches("") || auxpanmc.matches("") || auxprdmc.matches("") || auxmpp.matches("")
            || auxmp.matches("") || auxmpes.matches("") || auxpp.matches("") || auxc1.matches("") || auxnulos.matches("") || auxnr.matches("") || auxt.matches("")) {
        double pri = 0;
        double pan = 0;
        double prd = 0;
        double pt = 0;
        double pv = 0;
        double mc = 0;
        double panal = 0;
        double morena = 0;
        double pes = 0;
        double pri_pv = 0;
        double pan_prd_mc = 0;
        double pan_prd = 0;
        double pan_mc = 0;
        double prd_mc = 0;
        double morena_pt_pes = 0;
        double morena_pt = 0;
        double morena_pes = 0;
        double pt_pes = 0;
        double c1 = 0;
        double nulos = 0;
        double noregistrados = 0;
        double total = 0;

    }else{
            final int pri = Integer.parseInt(etpri.getText().toString());
            final int pan = Integer.parseInt(etpan.getText().toString());
            final int prd = Integer.parseInt(etprd.getText().toString());
            final int pt = Integer.parseInt(etpt.getText().toString());
            final int pv = Integer.parseInt(etpv.getText().toString());
            final int mc = Integer.parseInt(etmc.getText().toString());
            final int panal = Integer.parseInt(etpanal.getText().toString());
            final int morena = Integer.parseInt(etmorena.getText().toString());
            final int pes = Integer.parseInt(etpes.getText().toString());
            final int pri_pv = Integer.parseInt(etpri_pv.getText().toString());
            final int pan_prd_mc = Integer.parseInt(etpan_prd_mc.getText().toString());
            final int pan_prd = Integer.parseInt(etpan_prd.getText().toString());
            final int pan_mc = Integer.parseInt(etpan_mc.getText().toString());
            final int prd_mc = Integer.parseInt(etprd_mc.getText().toString());
            final int morena_pt_pes = Integer.parseInt(etmorena_pt_pes.getText().toString());
            final int morena_pt = Integer.parseInt(etmorena_pt.getText().toString());
            final int morena_pes = Integer.parseInt(etmorena_pes.getText().toString());
            final int pt_pes = Integer.parseInt(etpt_pes.getText().toString());
            final int c1 = Integer.parseInt(etc1.getText().toString());
            final int nulos = Integer.parseInt(etnulos.getText().toString());
            final int noregistrados = Integer.parseInt(etnoregistrados.getText().toString());
            final int total = Integer.parseInt(ettotal.getText().toString());


            Response.Listener<String> respoListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if (success) {

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

        String REGISTER_REQUEST_URL = getString(R.string.url_prep) + "/peticiones/partidos.php";
        //String REGISTER_REQUEST_URL =  "http://ptsv2.com/t/dbuwef/post";

        RegisterRequest registerRequest = new RegisterRequest(REGISTER_REQUEST_URL, pri, pan, prd, pt, pv, mc, panal,
                morena, pes, pri_pv, pan_prd_mc, pan_prd, pan_mc, prd_mc, morena_pt_pes, morena_pt, morena_pes, pt_pes,
                c1, nulos, noregistrados, total, casillaseleccionada, respoListener);
        RequestQueue queue = Volley.newRequestQueue(RegistroFragment.this);
        queue.add(registerRequest);
    }
}
0

una manera fácil de hacer esto, sin complicarte mucho es

final int pri = etpri.getText().toString().isEmpty() ? 0 : Integer.parseInt(etpri.getText().toString());

repite lo mismo para todos lo campos que tienes. Es como un if para cada uno, si el campo de texto esta vacío devolverá 0, sino devolverá lo que tiene el campo de texto

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.