Android Open Source - SimpleNotes Note Data Base Helper






From Project

Back to project page SimpleNotes.

License

The source code is released under:

Apache License

If you think the Android project SimpleNotes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.moysa.simplenotes.db;
// w  w w  .j  av a2  s  .  c o m
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.moysa.simplenotes.core.Note;
import com.moysa.simplenotes.core.NotesUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sergey Moysa on 27.02.14.
 */
public class NoteDataBaseHelper extends SQLiteOpenHelper {

    private static final String LOG_TAG = "database";
    public static final String NOTES = "Notes";
    public static final String NOTE_ID = "id";
    public static final String NOTE_NAME = "name";
    public static final String NOTE_MESSAGE = "message";
    public static final String NOTE_DATE = "date";

    public NoteDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table Notes ("
                + NOTE_ID + " integer primary key autoincrement,"
                + NOTE_NAME + " text,"
                + NOTE_MESSAGE + " text,"
                + NOTE_DATE + " datetime default (datetime('now', 'localtime')));");


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void putNote(Note note) {

        SQLiteDatabase database = getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(NOTE_NAME, note.getName());
        values.put(NOTE_MESSAGE, note.getMessage());

        long rowID = database.insert(NOTES, null, values);
        note.setId(rowID);
        note.setDate(getNoteDate(rowID));
        Log.d(LOG_TAG, "Row " + note.getName() + " inserted. ID: " + rowID);
    }

    private String getNoteDate(long rowID) {

        String date = null;
        SQLiteDatabase database = getReadableDatabase();
        Cursor c = database.query(NOTES, new String[]{NOTE_DATE}, NOTE_ID + " = " + rowID, null, null, null, null);

        if (c != null) {
            c.moveToFirst();
            date = c.getString(c.getColumnIndex(NOTE_DATE));
        }

        Log.d(LOG_TAG, "Date " + date + " pulled.");

        return date;
    }

    public List<Note> getNotes() {

        ArrayList<Note> notes = new ArrayList<Note>();
        SQLiteDatabase database = getReadableDatabase();
        Cursor c = database.query(NOTES, null, null, null, null, null, null);

        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    int id = c.getInt(c.getColumnIndex(NOTE_ID));
                    String name = c.getString(c.getColumnIndex(NOTE_NAME));
                    String message = c.getString(c.getColumnIndex(NOTE_MESSAGE));
                    String date = c.getString(c.getColumnIndex(NOTE_DATE));

                    notes.add(new Note(id, name, message, date));
                } while (c.moveToNext());
            }

            c.close();
        }

        //TODO sort not here and use preferences
        NotesUtils.sortNotes(notes, false, false);

        return notes;
    }

    public void deleteNote(Note note) {

        SQLiteDatabase database = getWritableDatabase();
        int rows = database.delete(NOTES, NOTE_ID + " = " + note.getId(), null);

        Log.d(LOG_TAG, "Row " + note.getName() + " deleted");
    }

    public void updateNote(Note note) {

        SQLiteDatabase database = getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(NOTE_NAME, note.getName());
        values.put(NOTE_MESSAGE, note.getMessage());

        int rows = database.update(NOTES, values, NOTE_ID + " = " + note.getId(), null);
        Log.d(LOG_TAG, "Row " + note.getName() + " updated.");
    }
}




Java Source Code List

com.moysa.simplenotes.core.Note.java
com.moysa.simplenotes.core.NotesComparator.java
com.moysa.simplenotes.core.NotesUtils.java
com.moysa.simplenotes.db.NoteDataBaseHelper.java
com.moysa.simplenotes.ui.activities.MainActivity.java
com.moysa.simplenotes.ui.activities.SettingsActivity.java
com.moysa.simplenotes.ui.adapters.NotesAdapter.java
com.moysa.simplenotes.ui.application.NoteApplication.java
com.moysa.simplenotes.ui.fragments.NavigationDrawerFragment.java
com.moysa.simplenotes.ui.fragments.NoteFragment.java
com.moysa.simplenotes.ui.listeners.ListHeaderAnimationListener.java