I am having an issue where the item is getting inserted to my db twice. I dont have 2 insert statements.
I am trying to check if an array’s element is empty.
Here are snippets of my code
In my main activity
private String[] dateTimeDispStr= new String[2];
in onCreate:
dateTimeDispStr[0]=null;
dateTimeDispStr[1]=null;
Other parts of same activity:
//-------------------------------------------update time----------------------------------------//
public void updatetime()
{
dateTimeDispStr[0]=new StringBuilder()
.append(pad(mhour)).append(":")
.append(pad(mminute)).toString();
tdv.editRow(this, dateTimeDispStr);
if(dateTimeDispStr[1]!=null){
update();
}
}
//-------------------------------------------update date----------------------------------------//
private void updateDate() {
dateTimeDispStr[1]=new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/")
.append(mDay).append("/")
.append(mYear).append(" ").toString();
tdv.editRow(this, dateTimeDispStr);
if(dateTimeDispStr[0]!=null){
update();
}
}
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:MM");
public void update(){
Calendar cal = Calendar.getInstance();
cal.set(mYear, mMonth + 1, mDay, mhour, mminute, 0);
String date = formatter.format(cal.getTime());
tdi.setDate(date.split(" ")); // split makes an array of ["date", "time"]
db.addItem(tdi);
Log.d("Item set in the db", "The item: " + date);
}
Here is my database handler
// Adding new ToDoItem
public void addItem(ToDoItem item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TASK, item.getTDItem()); // item task
values.put(KEY_DATE, item.getDate()); // item date
values.put(KEY_PRIORITY, item.getPriority()); // item priority
// Inserting Row
db.insert(TABLE_TDL, null, values);
db.close(); // Closing database connection
}
tdv.editRow edits the row on my UI that is extending a table layout. I will add the functionality for checking for null in a sec. It essentially appends the row that already had the task of the todo item, but with date and time I would like to add them to that row
I understand that I call update in both methods, but that is because if the user clicks either button first.
Instead of all that I would simply fetch a Calendar instance, populate it with your date / time variables, and use a DateFormat to build a localized String.
Try this
update()by itself:(I don’t have a working compiler at the moment so forgive any minor flaws…)
Read about customizing the SimpleDateFormat string here.