Vnge
Vnge

Reputation: 1305

SQLite is inserting data entry twice

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.

Upvotes: 1

Views: 1664

Answers (2)

Sam
Sam

Reputation: 86958

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:

// Create a field variable
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm"); // HH for 24hr

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);
}

(I don't have a working compiler at the moment so forgive any minor flaws...)
Read about customizing the SimpleDateFormat string here.

Upvotes: 1

Elior
Elior

Reputation: 3266

Yes, you don't have 2 insert statements but you're calling the addItem() method twice.

lets say you need to update the date and the time.. from your code when you're updating the time.. you call the update() method which calls the addItem() method that insert the item to DB. next, when you're updating the date of the same item, you call the update() again which calls the addItem() method that insert the item once again to the DB.

Hope this was helpful

Upvotes: 0

Related Questions