-2

I am absolute beginner to Android. I have 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. 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;
    }
}

This is my complete 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();
    }
}

Why is my date value always null in database? The input format of the text field is MM/dd/yyyy for date.

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

2 Answers2

0

I had this issue once, and I was crazy, my particular problem was the format of the Date. First of all check again this line, and make sure it have getting values: String date = tfDate.getText().toString();

If is right, format the Date to this "yyyy-MM-dd" before insert into the database.

Something like that (Inside Task class):

public String getDateStr()
{
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String date = dateFormat.format(this.TaskDate);
    return date;
}
KronuZ
  • 378
  • 2
  • 13
0

I tried so many ways. I tried to insert date by formatting to yyyy-MM-dd. But it is not working properly.

The best way to do it are:

1- Convert the data type of taskDate property of Task class to String whereas the data type for that in the database is Date. But it is a recommanded way. It works perfect.

2- Store the date as BIGINT in database. So the data type for property of Task class will be Long. This is also recommaned way.

I used the first option.

Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372