how to validate those that do not have empty data in Android Studio Form

1

This is the code:

AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper
                        (Alta_Alumno.this, "administrations", null, 1);

                SQLiteDatabase bd = admin.getWritableDatabase();

                String Matricula = etm.getText().toString();
                String Nombre = etn.getText().toString();
                String ApPaterno = etAp.getText().toString();
                String ApMaterno = etAm.getText().toString();
                String Direccion = etd.getText().toString();

                ContentValues registro = new ContentValues();

                    registro.put("Matricula", Matricula);
                    registro.put("Nombre", Nombre);
                    registro.put("ApPaterno", ApPaterno);
                    registro.put("ApMaterno", ApMaterno);
                    registro.put("Direccion", Direccion);
                    bd.insert("Alumno", null, registro);

                    bd.close();
                    etm.setText("");
                    etn.setText("");
                    etAp.setText("");
                    etAm.setText("");
                    etd.setText("");
                    Toast.makeText(Alta_Alumno.this, "Datos del usuario guardados", Toast.LENGTH_LONG).show();
    
asked by Pako Hdz 25.10.2017 в 23:33
source

2 answers

1
SQLiteDatabase bd = admin.getWritableDatabase();
String Matricula = etm.getText().toString();
String Nombre = etn.getText().toString();
String ApPaterno = etAp.getText().toString();
String ApMaterno = etAm.getText().toString();
String Direccion = etd.getText().toS

if(Matricula.getText().toString()=="" || Nombre.getText().toString()=="" || ApPaterno.getText().toString()=="" || ApMaterno.getText().toString()=="" || Direccion.getText().toString()==""){
     ContentValues registro = new ContentV
     registro.put("Matricula", Matricula);
     registro.put("Nombre", Nombre);
     registro.put("ApPaterno", ApPaterno);
     registro.put("ApMaterno", ApMaterno);
     registro.put("Direccion", Direccion);
     bd.insert("Alumno", null, re
     bd.close();
etm.setText("");
etn.setText("");
etAp.setText("");
etAm.setText("");
etd.setText("");
Toast.makeText(Alta_Alumno.this, "Datos del usuario guardados", Toast.LENGTH_LONG).show();

}else{

    Toast.makeText(Alta_Alumno.this, "un campo esta vacio, y no se pudo guardar el alumno", Toast.LENGTH_LONG).show();
}

Something simple but it can work

    
answered by 25.10.2017 в 23:54
1

You can use isEmpty () to check if the EditText has no content, and trim () to remove spaces at the ends of the text, for example to validate the EditText to store the "enrollment":

if(etm.getText().toString().trim().isEmtpy()){
   //No tiene valor.
}else{
   //Tiene valor.
}

To ensure that all EditText have value you can do it this way:

 if(etm.getText().toString().trim().isEmtpy() 
                || etn.getText().toString().trim().isEmpty()
                || etAp.getText().toString().trim().isEmpty()
                || etAm.getText().toString().trim().isEmpty()
                || etd.getText().toString().trim().isEmpty()){

 //falta algún valor.

}else{

 //Se tienen todos los valores.

}
    
answered by 25.10.2017 в 23:54