Toast validation and seat insertion

Asked

Viewed 254 times

1

I’m taking a risk on Android, I’m very beginner and I’m having problems with my first entry in the database. I also need to validate the fields of my form before sending. If possible, a touch of good practice will also go well.

Acquisition of the form data and call the method to save the data in the database:

public void GetActionValues(View objView) {
        EditText etProjectName = (EditText) findViewById(R.id.txtProjectName);
        EditText etCustomerName = (EditText) findViewById(R.id.txtCustomerName);
        EditText etCustomerEmail = (EditText) findViewById(R.id.txtCustomerEmail);
        EditText etCustomerPhone = (EditText) findViewById(R.id.txtCustomerPhone);

        Project project = new Project();


        /* VALIDAÇÃO DE CAMPOS */
        if (!TextUtils.isEmpty(etProjectName.getText().toString()))
            project.Name = etProjectName.getText().toString();      
        else {
            Toast.makeText(this, R.string.msg_validation_ProjectName, Toast.LENGTH_SHORT).show();
            etProjectName.findFocus();
        }
        if (!TextUtils.isEmpty(etCustomerName.getText().toString())) 
            project.CustomerName = etCustomerName.getText().toString();     
        else {
            Toast.makeText(this, R.string.msg_validation_CustomerName, Toast.LENGTH_SHORT).show();
            etCustomerName.findFocus();
        }
        project.CustomerEmail = etCustomerEmail.getText().toString();
        project.CustomerPhone = etCustomerPhone.getText().toString();

        SaveProject(project);
    }

    public void SaveProject(Project projectObject) {
        ContentValues values = new ContentValues();
        values.put("Name", projectObject.Name);
        values.put("CustomerName", projectObject.CustomerName);
        values.put("CustomerEmail", projectObject.CustomerEmail);
        values.put("CustomerPhone", projectObject.CustomerPhone);
        _context.insert("Project", null, values);

        Toast.makeText(this, R.string.msg_project_save_success,
                Toast.LENGTH_SHORT).show();
    }

In this part, debugging the code I notice that it enters the Else in the validation and does not display the messages of Toast, going through the Else but without showing anything and nor giving the focus in the field.

Model DBHelper:
public class DBHelper extends SQLiteOpenHelper {

    private final static String CREATE_SQL_Profile_TABLE = "CREATE TABLE Profile (_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
                                                            "CostHour FLOAT NOT NULL)" ;

    private final static String CREATE_SQL_Project_TABLE = "CREATE TABLE Project (_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
                                                            "Name VARCHAR(50) NOT NULL, " +
                                                            "CustomerName VARCHAR(50) NOT NULL," +
                                                            "CustomerEmail VARCHAR(50)," +
                                                            "CustomerPhone VARCHAR(50)," +
                                                            "WorkingTime VARCHAR(50)," +
                                                            "AmountMoney FLOAT)" ;

    private final static String DELETE_SQL_Profile_TABLE = "DROP TRABLE Profile";

    private final static String DELETE_SQL_Project_TABLE = "DROP TRABLE Project";

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        if(db != null){     
            db.execSQL(CREATE_SQL_Profile_TABLE);
            db.execSQL(CREATE_SQL_Project_TABLE);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

        db.execSQL(DELETE_SQL_Profile_TABLE);
        db.execSQL(DELETE_SQL_Project_TABLE);
        db.execSQL(CREATE_SQL_Profile_TABLE);
        db.execSQL(CREATE_SQL_Project_TABLE);
    }

    public DBHelper(Context context, String name, int version){
        super(context, name, null, version);
    }
}

Debug: inserir a descrição da imagem aqui

Note that not all parameters of the form are coming, this missing the "Customername", which in SQL can not be null. But the procedure seems to be correct.

  • Instead of .findFocus(); must be .requestFocus();

  • when you call R.string.msg_validation_ProjectName, it will return you an integer that references this string in your Resources. Use instead getString(R.string.msg_validation_ProjectName) and also use . requestFocus();` as @ramaral said.

  • some put the answer :) it worked yes! Thanks!

1 answer

1


Some tips:

In the method GetActionValues(View objView) two checks are being made in sequence, if the two fields (Name and Customername) are invalid, one Toast will be sent on top of the other, and therefore only the second Toast will be visible.


A validation could also be made that if a required field (Name or Customername for example) is invalid, do not call the method SaveProject(). The way it is implemented if the CustomerName is null, a Toast will be shown, Edittext will request the focus, but still the insert at the bank will be called.
Last tip, following a naming convention, method names should start with lower case. If you want to read more about this: http://www.devmedia.com.br/convencoes-de-codigo-java/23871

Browser other questions tagged

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