insert the default note in the DB at creating

This commit is contained in:
tibbi 2016-09-27 22:28:32 +02:00
parent a4a4084260
commit c5785752ae
5 changed files with 56 additions and 6 deletions

View File

@ -33,6 +33,7 @@ dependencies {
compile 'com.jakewharton:butterknife:8.0.1'
compile 'com.github.yukuku:ambilwarna:2.0.1'
compile 'com.android.support:design:23.4.0'
compile 'com.facebook.stetho:stetho:1.4.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

View File

@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.simplemobiletools.notes"
xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simplemobiletools.notes">
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/launcher"
android:label="@string/app_launcher_name"

View File

@ -0,0 +1,12 @@
package com.simplemobiletools.notes;
import android.app.Application;
import com.facebook.stetho.Stetho;
public class App extends Application {
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
}

View File

@ -19,8 +19,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends SimpleActivity {
@BindView(R.id.notes_view)
EditText mNotesView;
@BindView(R.id.notes_view) EditText mNotesView;
private SharedPreferences mPrefs;

View File

@ -1,17 +1,26 @@
package com.simplemobiletools.notes.databases;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.simplemobiletools.notes.Constants;
import com.simplemobiletools.notes.models.Note;
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "notes.db";
private static final int DB_VERSION = 1;
private static final String TABLE = "notes";
private static final String NOTE = "General note";
private static final String COL_ID = "id";
private static final String COL_NAME = "name";
private static final String COL_TEXT = "text";
private static final String COL_TEXT = "value";
private Context mContext;
private SQLiteDatabase mDb;
public static DBHelper newInstance(Context context) {
return new DBHelper(context);
@ -19,20 +28,48 @@ public class DBHelper extends SQLiteOpenHelper {
private DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
mContext = context;
mDb = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE + "(" +
db.execSQL("CREATE TABLE " + TABLE + " (" +
COL_ID + " INTEGER PRIMARY KEY, " +
COL_NAME + " TEXT, " +
COL_TEXT + " TEXT" +
")"
);
insertFirstNote(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void insertFirstNote(SQLiteDatabase db) {
final SharedPreferences prefs = mContext.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
final String text = prefs.getString(Constants.TEXT, "");
final Note note = new Note(0, NOTE, text);
insertNote(note, db);
}
private void insertNote(Note note, SQLiteDatabase db) {
final ContentValues values = fillContentValues(note);
db.insert(TABLE, null, values);
}
public void insertNote(Note note) {
final ContentValues values = fillContentValues(note);
mDb.insert(TABLE, null, values);
}
private ContentValues fillContentValues(Note note) {
final ContentValues values = new ContentValues();
values.put(COL_NAME, note.getName());
values.put(COL_TEXT, note.getText());
return values;
}
}