I am absolute beginner to Android. But I am having a problem with working with date in Android. Now I am inserting a value from EditText field to a Sqlite table column that is date database date. It is always null when it is added to database. Please help me. What is wrong with my code ?
My DatabaseHelper class
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "todo.db";
private static final String TABLE_NAME = "task";
private static final String COLUMN_ID = "id";
private static final String COLUMN_DESCRIPTION = "description";
private static final String COLUMN_DATE ="date";
private static final String COLUMN_DONE = "done";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COLUMN_DESCRIPTION+" TEXT,"+
COLUMN_DATE+" DATE,"+COLUMN_DONE+" BOOLEAN)";
SQLiteDatabase db;
public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
this.db = db;
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
public void insertTask(Task task)
{
db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_DESCRIPTION,task.getDescription());
values.put(COLUMN_DATE,task.getDate().toString());
values.put(COLUMN_DONE,Boolean.FALSE.toString());
db.insert(TABLE_NAME, null, values);
db.close();
}
}
This is my save method in Fragment
public void saveTask()
{
String description = tfDescription.getText().toString();
String date = tfDate.getText().toString();
if(description.isEmpty())
{
Toast.makeText(getActivity().getBaseContext(),"Description is required",Toast.LENGTH_SHORT).show();
}
else if(date.isEmpty())
{
Toast.makeText(getActivity().getBaseContext(),"Date is required",Toast.LENGTH_SHORT).show();
}
else if(description.length()<getResources().getInteger(R.integer.min_description_length))
{
String minChar = String.valueOf(getResources().getInteger(R.integer.min_description_length));
Toast.makeText(getActivity().getBaseContext(),"Description should be minium "+minChar+" characters",Toast.LENGTH_SHORT).show();
}
else{
//check date
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
boolean parseOk = false;
Date taskDate = new Date();
try{
taskDate = format.parse(date);
Task task = new Task();
task.setDescription(description);
task.setDate(taskDate);
dbHelper.insertTask(task);
parseOk = true;
}
catch(ParseException e)
{
parseOk = false;
}
if(parseOk)
{
//insert task to database
Toast.makeText(getActivity().getBaseContext(),"Task saved",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getActivity().getBaseContext(),"Invalid date format",Toast.LENGTH_SHORT).show();
}
}
}
This is my Task class
public class Task {
private int Id;
private String Description;
private java.util.Date TaskDate;
private boolean Done;
public void setId(int Id)
{
this.Id = Id;
}
public int getId()
{
return this.Id;
}
public void setDescription(String Description)
{
this.Description = Description;
}
public String getDescription()
{
return this.Description;
}
public void setDate(Date taskDate)
{
this.TaskDate = taskDate;
}
public Date getDate(){
return this.TaskDate;
}
public void setDone(Boolean done)
{
this.Done = done;
}
public Boolean getDone()
{
return this.Done;
}
}
Please help me why my date value is always null in database. The input format of the text field is MM/dd/yyyy for date. Please what is wrong with my code ?
Aucun commentaire:
Enregistrer un commentaire