mercredi 27 avril 2016

Android: how do I fix timestamp addition to my SQLite database?

I am trying to add a TIMESTAMP Column to my SQLite database. The column is to be used to capture timestamp data using "System.currentTimeMillis();". App is crashing and the error is from the cursor code shown below in the line with ** **. The error reads "Unable to start activity ComponentInfo{...ListActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 6 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it."

Initially I set up the variable as a String in the model file. Then I tried a long and neither worked. What am I missing here?

UserData file:

...
public long getTimestamp() {
    return timestamp;
}

public void setTimestamp(long timestamp) {
    this.timestamp = timestamp;

DatabaseHelper.java file:

...
private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE IF NOT EXISTS " + DBContract.DBEntry.TABLE_NAME +
            "(" + DBContract.DBEntry.COLUMN_NAME_ID +
                  " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + DBContract.DBEntry.COLUMN_NAME_TODO +
                  " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_NOTE1 +
                  " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_NOTE2 +
                  " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_DUEDATE +
                  " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_DUETIME +
                " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_TIMESTAMP +
                  " TEXT" + ")";
...
Cursor cursor = db.rawQuery(query,null);

        try {
            if (cursor.moveToFirst()) {
                do {
                    UserData userData = new UserData();
                    userData.setTodo(cursor.getString(1));
                    userData.setNote1(cursor.getString(2));
                    userData.setNote2(cursor.getString(3));
                    userData.setDuedate(cursor.getString(4));
                    userData.setDuetime(cursor.getString(5));
                    **userData.setTimestamp(cursor.getLong(6));**

                    modelList.add(0, userData);
                    } while (cursor.moveToNext());
...

ListAdapter.java file:

...
public void onBindViewHolder(final ListViewHolder holder, final int position) {
    ...
    holder.cardBlankText5.setText((int) dbList.get(position).getTimestamp());

Activity.java file:

...
public void onClickSave(View v) {
    ...
    long timestamp=System.currentTimeMillis();
    helper = new DatabaseHelper(Activity.this);
    helper.insertIntoDB(todo,note1,note2,duedate,duetime,timestamp);
    startActivity(new Intent(Activity.this,ListActivity.class));
}

Aucun commentaire:

Enregistrer un commentaire