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:
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();
– ramaral
when you call
R.string.msg_validation_ProjectName
, it will return you an integer that references this string in your Resources. Use insteadgetString(R.string.msg_validation_ProjectName)
and also use . requestFocus();` as @ramaral said.– Ighor Augusto
some put the answer :) it worked yes! Thanks!
– Luiz Negrini