I'm risking Android, I'm very beginner and I'm having problems with my first insertion into the database. I also need to validate the fields on my form before sending. If possible, a touch of good practice goes well too.
Acquiring the form data and calling 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 notices that it enters the else in the validation and does not display the Toast messages, passing through the else but without showing anything or focusing on 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:
Notethatalltheparametersoftheformarenotarriving,itismissingthe"CustomerName", which in SQL can not be null. But the procedure seems to be correct.